yougene wrote:Is it possible to use setf without side effects if it's used to only modify variables passed in as parameters or generated within the function?
Is that how you write good lisp code?
You are right, setf
will only have side effects if you use it to modify a global variable or a local variable outside of the current function (like, when creating a function, you can modify the values being defined in its surroundings). Sometimes, whether you use setf
is just a matter of personal preference. For instance when you want your function to accept numbers and strings representing numbers, one way you can do to make sure that the variable holds a number is setf
-ing it or rebinding it with let
In Lisp, we consider setf
to be a bad choice when it is not necessary. Lisp is about brevity, not walk on circles around what you are trying to do. In C, programmers often assign the result of a function just to pass it to another function, and they do that when they learn Lisp, which is considered bad Lisp. It is considered to be more Lispy style to nest the function calls, unless the nesting get too haired, in which case you generally use let
, but a setf
can also be used if it makes sense.
Also, in general, Lispers avoid side effects, but sometimes they are very useful. There are other languages where side effects are condemned with punishment, but that is not the case with Common Lisp