[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
- To: common-lisp@su-ai.ARPA, gls@think-aquinas.ARPA, vrotney@isi-vax.ARPA
- Subject: loop macro
- From: Liz Allen <firstname.lastname@example.org>
- Date: Tue, 04 Feb 1986 16:16:00 -0000
- Cc: Bruce Israel <email@example.com>
- In-reply-to: Your message of Thu, 23 Jan 86 17:45:08 EST. <8601232245.AA22847@brillig.umd.edu>
This is in response to a request for a loop macro that will run in
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
prints: 3 4 5
(for n from 0 to 13
when (> n 6)
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.