Common Lisp/External libraries/Extended Binding

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Binding with Metabang-Bind[edit | edit source]

Metabang-Bind combines several of the different lexical binding techniques into one facility. The types of binds it currently supports are LET, DESTRUCTURING-BIND, MULTIPLE-VALUE-BIND, binding on property lists, binding to structure slots, binding to class slots (with the option of using accessors), and destructuring arrays. In addition, BIND is also extensible via CLOS, so you can introduce your own binding methods.

Further reading[edit | edit source]

Anaphoric macros with Anaphora[edit | edit source]

Anaphora is a library of macros that implement anaphoric control structures. An anaphoric structure is one that implicitly binds a previous result. The meaning of this binding is found by looking at the context of the expression. As an example from the English language, consider the sentence, "Pick up the ball and throw it." It's quite clear that you would never say "Pick up the ball and throw the ball," as that would be redundant. However, most computer languages require you to express your thoughts in this redundant manner, as in:

(if (pseudo-p)
    (print (pseudo-p))
    (error "It is not the case that pseudo-p.") )

Now if the pseudo-predicate PSEUDO-P was really computationally intensive, you might not want to evaluate it twice. In such a case, you would probably end up binding the original result and using it later, like this:

(let ((result (pseudo-p)))
  (if result
      (print result)
      (error "It is not the case that pseudo-p.") ))

This is basically all that anaphoric structures gain you, but it can be quite convenient and creates code that is closer to how you would explain the procedure to a friend rather than a computer. With anaphora, one could write the example above as:

(aif (pseudo-p)
     (print it)
     (error "It is not the case that pseudo-p.") )

Anaphora also includes anaphoric versions of WHEN, UNLESS, COND, CASE, TYPECASE and a few others.

Anaphora also offers the idea of symbolic anaphoric structures. In these macros, the predicate form is saved, instead of its return value. This means that IT can act as a place in a SETF expression.

Further reading[edit | edit source]