World Library  
Flag as Inappropriate
Email this Article

Anaphoric macro

Article Id: WHEBN0033842863
Reproduction Date:

Title: Anaphoric macro  
Author: World Heritage Encyclopedia
Language: English
Subject: Macro (computer science), Source code, On Lisp, Lisp programming language, Hygienic macro
Publisher: World Heritage Encyclopedia

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.


The loop macro in ANSI Common Lisp is anaphoric in that it binds 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 which is 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))

 ;; Factorial function defined recursively where `self' refers to the alambda function
 (alambda (n) 
   (if (= n 0)
     (* n (self (1- n)))))

See also


  1. ^ a b Chapter 6 of Let over Lambda
  2. ^ 22. LOOP for Black Belts from Practical Common Lisp
  3. ^ What would be an example of an anaphoric conditional in Lisp? on StackOverflow
  4. ^ Examples of clause grouping from the Common Lisp HyperSpec
  5. ^ a b Chapter 14. Anaphoric Macros of On Lisp by Paul Graham

External links

  • Chapter 14. Anaphoric Macros from On Lisp by Paul Graham
  • Anaphora — an anaphoric macro collection
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.