Go to the first, previous, next, last section, table of contents.


let

The let expression is a special form in Lisp that you will need to use in most function definitions. Because it is so common, let will be described in this section.

let is used to attach or bind a symbol to a value in such a way that the Lisp interpreter will not confuse the variable with a variable of the same name that is not part of the function. To understand why this special form is necessary, consider the situation in which you own a home that you generally refer to as `the house', as in the sentence, "The house needs painting." If you are visiting a friend and your host refers to `the house', he is likely to be referring to his house, not yours, that is, to a different house. If he is referring to his house and you think he is referring to your house, you may be in for some confusion. The same thing could happen in Lisp if a variable that is used inside of one function has the same name as a variable that is used inside of another function, and the two are not intended to refer to the same value.

The let special form prevents this kind of confusion. let creates a name for a local variable that overshadows any use of the same name outside the let expression. This is like understanding that whenever your host refers to `the house', he means his house, not yours. (Symbols used in argument lists work the same way. See section The defun Special Form.)

Local variables created by a let expression retain their value only within the let expression itself (and within expressions called within the let expression); the local variables have no effect outside the let expression.

let can create more than one variable at once. Also, let gives each variable it creates an initial value, either a value specified by you, or nil. (In the jargon, this is called `binding the variable to the value'.) After let has created and bound the variables, it executes the code in the body of the let, and returns the value of the last expression in the body, as the value of the whole let expression. (`Execute' is a jargon term that means to evaluate a list; it comes from the use of the word meaning `to give practical effect to' (Oxford English Dictionary). Since you evaluate an expression to perform an action, `execute' has evolved as a synonym to `evaluate'.)


Go to the first, previous, next, last section, table of contents.