问题
What would be an example of an anaphoric conditional in Lisp? Please explain the code as well.
回答1:
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.
回答2:
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)
.
来源:https://stackoverflow.com/questions/3920193/what-would-be-an-example-of-an-anaphoric-conditional-in-lisp