197

What is the difference between "set", "setq", and "setf" in Common Lisp?

Jay
  • 9,585
  • 6
  • 49
  • 72
Richard Hoskins
  • 2,172
  • 2
  • 13
  • 9
  • 12
    The behavior of these is answered pretty well in the answers, but the accepted answer has a possibly mistaken etymology for the "f" in "setf". The answer to [What does the f in setf stand for?](http://stackoverflow.com/q/23808189/1281433) says that it's for "function", and provides references to back it up. – Joshua Taylor May 22 '14 at 15:09

6 Answers6

196

Originally, in 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 atrophied remains of older dialects, and will probably be booted from eventual successors of Common Lisp.

joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
Stack Programmer
  • 3,386
  • 1
  • 20
  • 12
  • 48
    Common Lisp always had lexical variables. You must be talking about some Lisp before Common Lisp. – Rainer Joswig May 15 '09 at 17:22
  • 4
    If SET and SETQ are to be booted from a Common Lisp successor, they will have to get some replacement. Their use in high-level code is limited, but low-level code (for example, the code SETF is implemented in) needs them. – Svante May 16 '09 at 19:53
  • 14
    is there a reason you chose 'car' as a field instead of something that might get confused as the car function? – drudru Jul 11 '10 at 04:35
  • 9
    [This answer](http://stackoverflow.com/a/23808420/1281433) to [What does the f in setf stand for?](http://stackoverflow.com/q/23808189/1281433) claims that the `f` actually stands for **function**, not **field** (or **form**, for that matter), and provides references, so while the setf for field makes some sense, it looks like it might not be correct. – Joshua Taylor May 22 '14 at 15:06
  • 1
    Summary: `set` is a function. Thus it does not know the environment. `set` cannot see lexical variable. It can set only the symbol-value of its argument. `setq` is not "set quoted" any more. The fact that `setq` is a special form, not a macro shows that. – KIM Taegyoon Jul 03 '18 at 14:36
176
(set ls '(1 2 3 4)) => Error - ls has no value

(set 'ls '(1 2 3 4)) => OK

(setq ls '(1 2 3 4)) => OK - make ls to (quote ls) and then have the usual set

(setf ls '(1 2 3 4)) => OK - same as setq so far BUT

(setf (car ls) 10) => Makes ls '(10 2 3 4) - not duplicated by setq/set
Community
  • 1
  • 1
Sourav
  • 1,769
  • 1
  • 10
  • 2
  • 18
    I find your answer to be more clear than the top voted one. Thanks a lot. – CDR Dec 09 '09 at 06:13
  • 2
    @Sourav, please NEVER use the letter "l" (ell) as a variable or symbol in example code. It is too hard to visually distinguish from the numeral 1. – DavidBooth Dec 20 '14 at 00:56
  • No, I still don't understand how (car ls) can be a l-value or not. Do you understand how to translate CLisp into C ? and how to write a CLisp interpreter ? – reuns Nov 16 '15 at 01:49
  • @user1952009 clisp is one Common Lisp implementation. If you want to refer to the language itself, CL the most used abbreviation. – ssice May 18 '16 at 13:35
  • @user1952009 To put it in C terms, `car` essentially dereferences the list "pointer", giving you a reference to the first item (`first` does the same thing). `(setf (car ls) 10)` might be written as `*ls = 10;` in C. – Yay295 Nov 13 '16 at 18:03
  • @Yay295 so you are saying it is a L-value, so that `(setf (car ls) 5)`is the same as `l->val = 5;` in C. What about `(setf (car (car (car ls))) 5)` if say `(setq ls '(((1))) )` ? – reuns Nov 13 '16 at 18:11
  • 2
    @user1952009 After `(setq ls '(((1))))`, `(setf (car (car (car ls))) 5)` is undefined behaviour, because the value of `ls` is constant (like modifying a string literal in C). After `(setq ls (list (list (list 1))))`, `(setf (car (car (car ls))) 5)` works just like `ls->val->val->val = 5` in C. – kyle Jun 13 '17 at 01:41
  • 1
    @DavidBooth You probably need a better font. Try this: https://github.com/tonsky/FiraCode – ceving Jan 08 '23 at 08:48
24

You can use setf in place of set or setq but not vice versa since setf can also set the value of individual elements of a variable if the variable has individual elements. See the exaples below:

All four examples will assign the list (1, 2, 3) to the variable named foo.

(set (quote foo) (list 1 2 3))    ;foo => (1 2 3)
(1 2 3)

(set 'foo '(1 2 3))   ;foo => (1 2 3) same function, simpler expression
(1 2 3)

(setq foo '(1 2 3))   ;foo => (1 2 3) similar function, different syntax
(1 2 3)

(setf foo '(1 2 3))   ;foo => (1 2 3) more capable function
(1 2 3)

setf has the added capability of setting a member of the list in foo to a new value.

foo                   ;foo => (1 2 3) as defined above
(1 2 3)

(car foo)             ;the first item in foo is 1
1

(setf (car foo) 4)    ;set or setq will fail since (car foo) is not a symbol
4

foo                   ;the fist item in foo was set to 4 by setf
(4 2 3)

However, you can define a symbol macro that reprents a single item within foo

(define-symbol-macro foo-car (car foo))    ; assumes FOO => (1 2 3)
FOO-CAR

foo-car               ;foo-car is now a symbol for the 1st item in foo
1

(setq foo-car 4)      ;set or setq can set the symbol foo-car 
4

foo                   ;Lisp macros are so cool
(4 2 3)

You can use defvar if you have not already defined the variable and do not want to give it a value until later in your code.

(defvar foo2)
(define-symbol-macro foo-car (car foo2))
dansalmo
  • 11,506
  • 5
  • 58
  • 53
22

setq is just like set with a quoted first arg -- (set 'foo '(bar baz)) is just like (setq foo '(bar baz)). setf, on the other hand, is subtle indeed -- it's like an "indirection". I suggest http://www.n-a-n-o.com/lisp/cmucl-tutorials/LISP-tutorial-16.html as a better way to get started understanding it than any answer here can give... in short, though, setf takes the first argument as a "reference", so that e.g. (aref myarray 3) will work (as the first arg to setf) to set an item inside an array.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
19

One can think of SET and SETQ being low-level constructs.

  • SET can set the value of symbols.

  • SETQ can set the value of variables.

Then SETF is a macro, which provides many kinds of setting things: symbols, variables, array elements, instance slots, ...

For symbols and variables one can think as if SETF expands into SET and SETQ.

* (macroexpand '(setf (symbol-value 'a) 10))

(SET 'A 10)


* (macroexpand '(setf a 10))         

(SETQ A 10)

So SET and SETQ are used to implement some of the functionality of SETF, which is the more general construct. Some of the other answers tell you the slightly more complex story, when we take symbol macros into account.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
5

I would like to add to previous answers that setf is macro that call specific function depending on what was passed as its first argument. Compare results of macro expansion of setf with different types of arguments:

(macroexpand '(setf a 1))

(macroexpand '(setf (car (list 3 2 1)) 1))

(macroexpand '(setf (aref #(3 2 1) 0) 1))

For some types of arguments "setf function" will be called:

(defstruct strct field)
(macroexpand '(setf (strct-field (make-strct)) 1))
Filipp
  • 859
  • 1
  • 6
  • 20