[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

loop macro



This is in response to a request for a loop macro that will run in
Common Lisp.

At Maryland, we've been using a for macro that provides a nice
looping macro and depends on almost nothing -- cond, setq, progn
and do is about all.  It is a version of the InterLisp for macro
with a number of new keywords.  If anyone would like the code for
it, let me know.  There are a some examples of using it and
descriptions of some of the other keywords below so you can get
some feeling for it.  It expands into a call to do with, possibly,
a let around it so it runs fairly efficiently.

(for x in '(a b c) do (print x))

(for x in '(a b c)
     y in '(d e f)
     collect (cons x y))
 ==> ((a . d) (b . e) (c . f))

(for x in '(a b c) join (list x 3))
 ==> (a 3 b 3 c 3)

(for x in '(a b 3 4 c 5)
     when (numberp x)
     sum (print x)		; all clauses may contain multiple expressions
	 x)
 prints: 3 4 5
 ==> 12

(for n from 0 to 13
     when (> n 6)
     quit n)
 ==> 7

To bind variables, you can use: "on" (same syntax as "in"),
destructuring with "in" (eg "(for (key . value) in assoc-list ...)"),
"let" to bind variables you want to see in later binding clauses, and
"bind" and "being" for other iteration variables.

Various conditions you can use are: "when", "unless", "while" and
"until".  The body keywords are: "do", "collect", "join", "sum",
"count", "alwyas", "never", "thereis", "last", "tcollect", "tjoin"
(like collect and join except using tconc cells) and "quit" (like
return).  There are also "initially" and "finally" clauses.

All the clauses that expect lisp bodies may have multiple clauses and 
an implicit progn is put around them.

The value of the for is kept in the local variable $$val -- that's
especially useful in the "finally" clause.


Enjoy!

				-Liz