LISP has the setf
function to assign a value to a variable. Now I have been wondering about the function's name: The set
part is obvious, but what does the f
suffix stand for?
setf is actually a macro that examines an access form and produces a call to the corresponding update function. Given the existence of setf in Common Lisp, it is not necessary to have setq, rplaca, and set; they are redundant. They are retained in Common Lisp because of their historical importance in Lisp.
What is the difference between setf and setq? Explanation: Setq is used only for setting the values of symbols but setf can be used for anything. Setf can be used for setting value of any data-type and not only symbols.
(setq var1 form1 var2 form2 ...) is the simple variable assignment statement of Lisp. First form1 is evaluated and the result is stored in the variable var1, then form2 is evaluated and the result stored in var2, and so forth. setq may be used for assignment of both lexical and dynamic variables.
The let expression is a special form in Lisp that you will need to use in most function definitions. let is used to attach or bind a symbol to a value in such a way that the Lisp interpreter will not confuse the variable with a variable of the same name that is not part of the function.
The actual meaning of F is often forgotten. According to some sources, f suffix could stand for:
However, according to Gabriel and Steele's The Evolution of Lisp, SETF comes from Peter Deutsch's A Lisp Machine with Very Compact Programs (published in 1973) and F stands for function.
In this paper, Peter Deutsch wrote:
The SET function is extended so that so that if the first argument is a list
(fn argl ... argn)
rather than a variable, the function fn is called in "store" mode with argumentsargl ... argn
and newvalue (the second argument of SET). SETQ is also extended in the obvious way, but is not particularly useful. A more useful function is(SETFQ (fn argl ... argn) newvalue)
which quotes the function name and evaluates everything else. This allows RPLACA, for example, to be defined as(LAMBDA (X Y) (SETFQ (CAR X) Y))
.
(emphasis mine)
Gabriel and Steele explain how this became SETF:
This name was abbreviated to SETF in Lisp-Machine Lisp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With