I’m pretty new to lisp, so apologies for what may be a simple question,
Whilst I understand the difference between DEFVAR and DEFPARAMETER (defvar only sets undefined variables), and the LET is for local scope only, what is the is the use of SETF as opposed to the other, previously mentioned assignment functions?
DEFVAR and DEFPARAMETER define and set global special (dynamically bound) variables.
SETF and SETQ set variables (global or local, special or lexical), but don’t define them.
SETF has more capabilities than SETQ, since it can set ‘places’ (like elements in lists, arrays, object’s slots, …).
Edit:
Paul says that in CLISP SETF defines the variable. That’s not quite what it does. Let’s have a look:
We have the following program:
Now let’s see what CLISP says if we compile that program:
In both functions CLISP warns us that the variable BAZ is neither declared not bound. Instead of rejecting the code, CLISP treats the variable BAZ as it were declared special.
Does that change if we load and run the code?
Nope, even after executing the SETF statements, CLISP thinks that the variable BAZ is not declared.
The SBCL compiler has this to say:
The LispWorks compiler has this to say: