Wednesday, April 18, 2012

Place forms

Common Lisp supports place forms with the setf function:
(setf (first coll) 10)
(setf (rest coll) '(20 30))
Place forms provide an elegant mechanism for handling parts of structures. This is arguably the most important distinguish feature of Common Lisp. Since Common Lisp is a Lisp-2, symbols have a symbol-function and a symbol-value part, which can be handled using setf:
(setf (symbol-function 'inc)
  (lambda (n)
    (+ n 1)))

(setf (symbol-value 'inc) 10)
An argument can be made in favor of Lisp-1's such as Clojure and Scheme, however, whatever the disadvantages of being a Lisp-2 are the use of place forms in Common Lisp make handling the parts of a symbol easy.

No comments:

Post a Comment

Post a Comment