Reputation: 2362
I learn Emacs Lisp, because I want to customize my editor and to be clear I am little bit stuck with how Dynamic binding works.
Here is example:
(setq y 2)
(let ((y 1)
(z y))
(list y z))
==> (1 2)
As a result I get back => (1 2) Please could some one explain what actually going on. I tried to explain it for my self using concept of frames where each frame create local binding, but it seems like here it works in different way.
Why it doesn't take closest value of 'y' in the nearest frame?
If could describe in details what is going here, I will be so happy.
Thanks in advance. Nick.
Upvotes: 1
Views: 133
Reputation: 17422
Your example seems to be taken straight from the Emacs Lisp Reference. If you scroll down to let*
, you'll get the explanation:
This special form is like let, but it binds each variable right after computing its local value, before computing the local value for the next variable. Therefore, an expression in bindings can refer to the preceding symbols bound in this let* form. Compare the following example with the example above for let:
(setq y 2)
⇒ 2
(let* ((y 1)
(z y)) ; Use the just-established value of y.
(list y z))
⇒ (1 1)
Upvotes: 3
Reputation: 21258
In emacs lisp (as in many lisps), the values that will be bound in let
are computed in parallel, in the environment "outside" the let
.
As an example, the following is (approximately) equivalent:
(let ((a b)
(b a))
...)
=>
(funcall (lambda (a b) ...) b a)
If you want to bind things in sequence, you should use let*
, which does what you expected let
to do.
Upvotes: 5
Reputation: 13477
The problem is solved if You use let*
which allows You to use the let
-mentioned vars:
(setq y 2)
(let* ((y 1)
(z y))
(list y z))
==> (1 1)
Upvotes: 2
Reputation: 4606
The value of y you are setting in the let us only in effect in the BODY of the let, it is not yet in effect yet when you set z in the same let statement.
Upvotes: 1