Why have the let function when you already have setf?

Discussion of Common Lisp
yougene
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

Why have the let function when you already have setf?

Post by yougene » Sat Jan 30, 2010 12:14 pm

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?

dmitry_vk
Posts: 96
Joined: Sat Jun 28, 2008 8:01 am
Location: Russia, Kazan
Contact:

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

Post by dmitry_vk » Sat Jan 30, 2010 12:56 pm

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.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

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

Post by gugamilare » Sat Jan 30, 2010 2:19 pm

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.

yougene
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

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

Post by yougene » Sat Jan 30, 2010 2:45 pm

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.

schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

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

Post by schoppenhauer » Sat Jan 30, 2010 3:45 pm

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.
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

yougene
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

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

Post by yougene » Sat Jan 30, 2010 4:46 pm

So each used variable has its own stack of possible bindings?

schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

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

Post by schoppenhauer » Sat Jan 30, 2010 4:51 pm

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.
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

yougene
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

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

Post by yougene » Sat Jan 30, 2010 4:55 pm

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?

schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

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

Post by schoppenhauer » Sat Jan 30, 2010 6:29 pm

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.
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

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

Post by Paul Donnelly » Sat Jan 30, 2010 6:40 pm

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.

Post Reply