views:

137

answers:

2

What's (in simple terms) the difference between setting a binding (LET) and symbols (=variables) in common lisp?

+4  A: 

Symbols and variables are two very different kinds of entities. Symbol is a name of something; variable is a container for a value. Variable can be named by a symbol.

Binding is an association between a symbol and a variable; when binding is in effect, you can refer to a variable by its name. let form creates such a binding.

dmitry_vk
Sorry If I wasn't clear enough.I was looking at this tutorial:http://en.wikibooks.org/wiki/Common_Lisp/First_steps/Beginner_tutorialWhat's the difference between:(let ((a 1)))and(setf a 1)
martins
(let ((a 1)) ...) creates a variable (with local lexical scope), a binding between symbol A and this variable (in the lexical context inside the "let" for) and initializes it to 1.(setf a 1) may behave differently: if there is a binding A, then it changes its value. If there is no binding, it creates a global binding A and initializes it to 1. However, the kind of global binding is left unspecified and differs between Lisp implementations (and their versions).So if you have a variable named A, setf will change its value; otherwise the result is not fully specified.
dmitry_vk
+1  A: 

(let ((a 1))) sets the value of a to 1 until the point where the closing bracket which matches the opening bracket before the let is reached, at which point a reverts to whatever it's previous value was (or becomes undefined). You often see a let in the body of a function where you require local variables which need to go out of scope at the end of the function, so you would use a let there.

(setf a 1) sets a to 1 and assumes that either a has been previously defined (whether by defparameter, defvariable or let) or that a is a new special variable that needs a value.

It's a bit more complicated than that but I'm not sure i have the lisp chops to explain it.

Amos