Programming

Difference between “set”, “setq”, and “setf” in Common Lisp?

steloflute 2013. 7. 31. 14:24
http://stackoverflow.com/questions/869529/difference-between-set-setq-and-setf-in-common-lisp


Originally, in Common Lisp, there were no lexical variables -- only dynamic ones. And there was no SETQ or SETF, just the SET function.

What is now written as:

(setf (symbol-value '*foo*) 42)

was written as:

(set (quote *foo*) 42)

which was eventually abbreviavated to SETQ (SET Quoted):

(setq *foo* 42)

Then lexical variables happened, and SETQ came to be used for assignment to them too -- so it was no longer a simple wrapper around SET.

Later, someone invented SETF (SET Field) as a generic way of assigning values to data structures, to mirror the l-values of other languages:

x.car := 42;

would be written as

(setf (car x) 42)

For symmetry and generality, SETF also provided the functionality of SETQ. At this point it would have been correct to say that SETQ was a Low-level primitive, and SETF a high-level operation.

Then symbol macros happened. So that symbol macros could work transparently, it was realized that SETQ would have to act like SETF if the "variable" being assigned to was really a symbol macro:

(defvar *hidden* (cons 42 42))
(define-symbol-macro foo (car *hidden*))

foo => 42

(setq foo 13)

foo => 13

*hidden* => (13 . 42)

So we arrive in the present day: SET and SETQ are athropied remains of older dialects, and will probably be booted from eventual successors of Common Lisp.




'Programming' 카테고리의 다른 글

[Java] Collections.shuffle을 이용한 랜덤처리  (0) 2013.08.02
The 90 Minute Scheme to C compiler  (0) 2013.08.01
Setting Up a newLISP Webserver  (0) 2013.07.30
UPenn Haskell Course  (0) 2013.07.30
A Taste of the λ Calculus  (0) 2013.07.23