4 ms·
and to directly answer your question, there a are many things you can do with let. Let binds a "form" to a symbol. (let ((pi 3.14)) ... code goes here
by flavio81 9y ago
and to directly answer your question, there a are many things you can do with let. Let binds a "form" to a symbol.
(let ((pi 3.14))
... code goes here ... )
So within the "code" part, that is, within that lexical environment, the symbol "pi" exists and is bound to float number 3.14
(Alternatively, you don't need to set a initial value.)
One of the interesting uses of let is that it can also redefine dynamic variables (aka special variables, think of them as "global variables).
So for example let's assume i define timeout to 300, so within my whole package, this special variable has the value of 300. Let's suppose this variable is going to be read by function "my-function", and others.
So i create this special variable:
(defparameter *timeout* 300)
Now, despite the above, let's suppose i want to call "my-function" but with a timeout of 1000. I can just do this in my code:
(let ((*timeout* 1000))
(my-function))
So, inside this "let", when "my-function" gets executed, the timeout will be 1000. Outside of this, it will still be 300.
Let, thus, allows you easy use of lexical environments.
Another use is creating closures by using the combination of "let" with "lambda."