What would be an example of an anaphoric conditional in Lisp? Please explain the code a开发者_开发技巧s well.
Paul Graham's On Lisp has a chapter on Anaphoric Macros.
Essentially, it's a shorthand way of writing statements that avoids repeating code. For example, compare:
(let ((result (big-long-calculation)))
(if result
(foo result)))
and
(if (big-long-calculation)
(foo it))
where it
is a special name that refers to whatever was just calculated in (big-long-calculation)
.
An example is the Common Lisp LOOP
:
(loop for item in list
when (general-predicate item)
collect it)
The variable IT
has the value of the test expression. This is a feature of the ANSI Common Lisp LOOP
facility.
Example:
(loop for s in '("sin" "Sin" "SIN")
when (find-symbol s)
collect it)
returns
(SIN)
because only "SIN"
is a name for an existing symbol, here the symbol SIN
. In Common Lisp symbol names have internally uppercase names by default.
精彩评论