Anaphoric macro

An anaphoric macro is a type of programming macro that deliberately captures some form supplied to the macro which may be referred to by an anaphor (an expression referring to another). Anaphoric macros first appeared in Paul Graham's On Lisp[1] and their name is a reference to linguistic anaphora[1]—the use of words as a substitute for preceding words.

Examples

The loop macro in ANSI Common Lisp is anaphoric in binding it to the result of the test expression in a clause.[2][3]

Here is an example that sums the value of non-nil elements, where it refers to the values of elements that do not equal nil:

 (loop for element in '(nil 1 nil 2 nil nil 3 4 6)
       when element sum it)
 ;; ⇒ 16

Here it is bound to the output of (and (> number 3) number) when true, collecting numbers larger than 3:[4]

 (loop for number from 1 to 6
       when (and (> number 3) number)
       collect it)                      ; IT refers to (and (> number 3) number).
 ;; ⇒ (4 5 6)

Defining anaphoric macros

One example is an anaphoric version of the if-then-else construct, which introduces an anaphor it, bound to the result of the test clause:[5]

 (defmacro aif (test-form then-form &optional else-form)
   `(let ((it ,test-form))
          (if it ,then-form ,else-form)))

 (aif (+ 2 7)
   (format nil "~A does not equal NIL." it)
   (format nil "~A does equal NIL." it))
 ;; ⇒ "9 does not equal NIL."

Another example is an anaphoric version of the λ-function, which binds the function itself to the anaphor self, allowing it to recur:[5]

 (defmacro alambda (parms &body body)
   `(labels ((self ,parms ,@body))
      #'self))

 ;; Factorial function defined recursively where `self' refers to the alambda function
 (alambda (n) 
   (if (= n 0)
     1 
     (* n (self (1- n)))))
gollark: Actually, over here at least, degrees lead to higher total earnings.
gollark: gollark Today at 12:53Logic puzzle:You are trapped in a labyrinth. There are some doors. One of them leads out. One of them leads into a lethal cryoapiary.There are two gollarks in front of the doors. One gollark speaks the truth, one gollark always lies. You suddenly notice other gollarks appearing. The other gollark tells the truth or lies at random. The other² gollark is truthful iff your question does not refer to itself or other gollarks. The other³ gollark calls in orbital laser strikes against those it perceives as asking tricky questions. The other⁴ gollark is truthful iff it predicts (with 99.6% historical accuracy) that you will consider it (one of) the falsehood-telling gollark(s). A subset of the gollarks will say "bee" and "apioform" instead of "true" or "false", but you do not know which or which words "bee" and "apioform" correspond to. The other⁴ gollark just tells you the first bit of the SHA256 hash of your question in UTF-8. Another gollark appears to be randomly materializing doors. The other⁵ gollark will cooperate with you iff you cooperate with CooperateBot/angel. Yet another gollark will tell the truth iff you know what iff means. The final gollark appears to be fiddling with the orbital mind control laser making you know this.What do you do?
gollark: I will copypaste the text maybe.
gollark: <@319753218592866315> What do?
gollark: And minoteaur for good measure.

See also

References

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.