Page 1 of 2

Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 12:14 pm
by yougene
Am I missing something here?

let can define a list of variables and setf can't( as far as I know ). Other than that I see no difference. Is this correct?

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 12:56 pm
by dmitry_vk
First of all, let and setf are not functions.
let and setf are completely different things.
setf is a generic assignment operator - it assigns values to "generic places". let is a special form that creates lexical or dynamic variable bindings.

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 2:19 pm
by gugamilare
Well, you should not create variables with setf (the behavior may vary depending on your implementation, and most of them create global (dynamic) variables). And let is also handy because you can assign a value to some variable and then restore the original value at the end. This is specially good when you have dynamic variables.

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 2:45 pm
by yougene
dmitry_vk wrote:First of all, let and setf are not functions.
let and setf are completely different things.
setf is a generic assignment operator - it assigns values to "generic places". let is a special form that creates lexical or dynamic variable bindings.
I'm not quite grasping the difference yet, but I'm getting there.


Thanks for the replies, they were informative.

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 3:45 pm
by schoppenhauer
Setf changes the value of an existing variable (well, it can create new ones, but it isnt supposed to do so) in the current scope, while let creates a new scope and - if necessary - overloads old variable namings by new ones. That is, let doesnt set any variables at all. Formally, you could think of a stack of variable-value-bindings, and let just pushes new ones on that stack, and pops them down again after leaving the let-macro, and the variable-binding being considered by the code is (mostly) the one topmost on this stack.

Let is stateless, while setf explicitly modifies a state. This can make a huge difference for example when working with multiple threads. In general, it is good practice to try to make code stateless, and only introducing states where it is really necessary.

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 4:46 pm
by yougene
So each used variable has its own stack of possible bindings?

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 4:51 pm
by schoppenhauer
yougene wrote:So each used variable has its own stack of possible bindings?
At least that is one possibility to imagine (and specify) what is being done. I.e., this is the model. The implementation itself is mostly different.

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 4:55 pm
by yougene
Let is stateless, while setf explicitly modifies a state. This can make a huge difference for example when working with multiple threads. In general, it is good practice to try to make code stateless, and only introducing states where it is really necessary.
Could you expand on this? Why is let stateless and setf isn't? Why have a variable at all if you're not going to modify it?


Would modifying lists be considered modifying a state?

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 6:29 pm
by schoppenhauer
yougene wrote:Could you expand on this? Why is let stateless and setf isn't?
Ok, sorry, "state" is not quite the concept - its about side effects. A let-statement overloads the value of a variable, but does not cause any side effects. See referential transparency for more details on this.
yougene wrote:Why have a variable at all if you're not going to modify it?
It depends on what you mean by "modify". Let overloads bindings of variables for a certain part of the program, without changing the Bindings for anything else. Of course, this modifies what a certain variable name stands for for a clearly defined part of the program code - so it modifies the variable somehow. But it doesnt do anything to the values this variable stands for.
yougene wrote:Would modifying lists be considered modifying a state?
Yes. Modifying a list means modifying a state. But modifying a list means modifying an object, not modifying a variable - i.e. thats something different.

Re: Why have the let function when you already have setf?

Posted: Sat Jan 30, 2010 6:40 pm
by Paul Donnelly
yougene wrote:Could you expand on this? Why is let stateless and setf isn't? Why have a variable at all if you're not going to modify it?
Perhaps you've completed a long computation and need to use the result multiple times. You don't want to recompute each time you need its value. Or perhaps you've called a function like GET-INTERNAL-REAL-TIME. It's probably very important that this value not change as the function is executed. Maybe you just need to abbreviate.
yougene wrote:Would modifying lists be considered modifying a state?
Yes.