[comp.lang.lisp] Select macro available was Help with Macros longish

sra@ecs.soton.ac.uk (Stephen Adams) (09/27/90)

In article <BEVAN.90Sep21154633@panda.cs.man.ac.uk> bevan@cs.man.ac.uk (Stephen J Bevan) writes:

 > [example of a pattern matching dispatch deleted]

 > This notation is particularly nice if you are writing simple
 > evaluators for languages.
 > [...]
 > Here's the problem.  I've tried to implement this in Lisp myself, but
 > to no avail. [...]

I have written and often use a macro called SELECT which
does a job like this.  Your example can be rewritten like
this:

 (defun eval-expr (x)
   (select x
     ('add x y) => (+ x y)
     ('sub x y) => (- x y)
     ('mul x y) => (* x y)
     ('div x y) => (/ x y)
   )
 )


I have made the source available by JANET ftp.
The site is uk.ac.soton.ecs and the file is
<FTP>lisp/select.lisp

The select macro is not only useful for writing
interpreters:

	(defun my-append (a b)
	  (select a
	    'nil      =>  b
            (hd . tl) =>  (cons hd (my-append tl b))))

	(defun fact (n)
	  (select n
             '0  =>  1
	     n   =>  (* n (fact (1- n)))))

The file also contains a macro called IN which is similar
to the `where' clause of SML and Miranda.
--
______________________________________________________________
Stephen Adams                        S.R.Adams@ecs.soton.ac.uk
Computer Science                     S.R.Adams@sot-ecs.uucp
Southampton University
Southampton SO9 5NH, UK