[Openmcl-devel] How to use lisp program in unix pipeline

Sven Van Caekenberghe sven at beta9.be
Mon May 12 08:00:38 PDT 2003

A long time ago, I was trying to do something similar. This is what I  
found (some old hacking) :

file: fac.lisp

;; factorial as a Unix command line application

(defun MAIN (args)
   "Application entry point"
   (format t "~d~%" (fac (read-from-string (first args))))

;; superfast fac
(declaim (ftype (function (integer integer) (values integer))
                 product-from-to binomial))
(defun product-from-to (aa bb)
   "Compute the product of integers from AA (EXclusive) to BB  
   (declare (integer aa bb))
   (when (> aa bb)
     (error "~s (~:d ~:d): the first argument must be smaller"
            'product-from-to aa bb))
   (when (minusp (* bb aa))
     (return-from product-from-to 0))
   ;; this algorithm insures that we multiply bignums
   ;; of approximately the same size
   ;; we use `labels' since some compilers optimize `labels' better than
   ;; plain recursion and because this avoids doing the above checks in  
   (labels ((pft (aa bb)
              (case (- bb aa)
                (0 1) (1 bb) (2 (* (- bb 1) bb)) (3 (* (- bb 2) (- bb 1)  
                (4 (* (- bb 3) (- bb 2) (- bb 1) bb))
                (t (let ((mm (ash (+ aa bb) -1)))
                     (* (pft aa mm) (pft mm bb)))))))
     (pft aa bb)))

(declaim (ftype (function (integer) (values integer)) fac))
(defun fac (nn)
   "Compute the factorial: n! = n * (n-1) * (n-2) * ..."
   (declare (integer nn))
   (product-from-to 1 nn))

Then file: fac.sh

# Invoke OpenMCL with any Lisp code as a Unix command line tool
# accepting all command line arguments

# Convert the arguments into a list of simple strings
arglst="( "
for i in $*
   arglst=$arglst\"$i\"" "

# Build the actual call expression
call="(main '$arglst)"

# Now invoke OpenMCL
openmcl -l fac -e "$call"

If openmcl in on your path, and if you make fac.sh executable (and  
place fac.lisp in the same directory), you can do this:

[sven at voyager:~/tmp]$ ./fac.sh 123

Maybe there are better ways to do the same thing (probably).

And indeed, OpenMCL's superfast startup time is great (one more reason  
to prefer openmcl over java, let alone the fact that openmcl does more  
in less memory and is at least as fast, if not faster)



On Monday, May 12, 2003, at 01:09 PM, Frank Sonnemans wrote:

> Hello all,
> I wrote a small lisp application which takes an xml file, filters it  
> and
> writes a html file. What I now want to do is use this lisp application  
> in
> several ways:
> Myfilter infile outfile
> Or
> Myfilter < infile > outfile
> Or
> Cat infile | Myfilter > outfile
> How do I do this with openmcl. As the startup time of openmcl is very  
> short
> I would basically like to use it to replace my unreadable perl scripts.
> So far I got some results using a construct like:
> Cat Myfilter.lisp | openmcl -b
> Problem is that openmcl outputs it's welcome message and prompt before  
> the
> output of my lisp program (see bottom of this message for example).  
> This is
> unsuitable for the type of use I have in mind. Can I suppress the  
> prompt and
> welcome message?
> Secondly how do I access the command line parameters from my lisp  
> program?
> Regards,
> Frank
> Example command line session showing openmcl printing prompt:
> [Frank-Sonnemanss-Computer:~] frank% cat test.lisp
> (print "hello world")
> [Frank-Sonnemanss-Computer:~] frank% cat test.lisp | openmcl -b
> Welcome to OpenMCL Version (Beta: Darwin) 0.13.4!
> ?
> "hello world"
> "hello world"

Sven Van Caekenberghe - mailto:sven at beta9.be
Beta Nine - software engineering - http://www.beta9.be
.Mac - svc at mac.com - http://homepage.mac.com/svc

Openmcl-devel mailing list
Openmcl-devel at clozure.com

More information about the Openmcl-devel mailing list