Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be an example of an anaphoric conditional in Lisp?

What would be an example of an anaphoric conditional in Lisp? Please explain the code as well.

like image 983
blunders Avatar asked Oct 13 '10 01:10

blunders


2 Answers

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).

like image 113
Greg Hewgill Avatar answered Nov 18 '22 23:11

Greg Hewgill


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.

like image 44
Rainer Joswig Avatar answered Nov 18 '22 23:11

Rainer Joswig