Reputation: 150902
I'm currently reading "Common Lisp: A Gentle Introduction to Symbolic Computation".
Chapter 5 introduces let
and let*
and discusses the differences between them, and especially points out that you may be tricked into thinking to always use let*
instead of let
, but you should not do this for two reasons:
let
is easier to understand because it implies that there are no dependencies.let
is the only correct choice, but it does not go into details.Actually, it says:
There are some situations where LET is the only correct choice, but we won't go into the details here. Stylistically, it is better to use LET than LET* where possible, because this indicates to anyone reading the program that there are no dependencies among the local variables that are being created. Programs with few dependencies are easier to understand.
So, now my question is: What are these situations where let
is the only correct choice?
Upvotes: 3
Views: 134
Reputation: 139401
It's mostly about which variable you want to refer to.
(let ((p 'foo))
(let or let* ; use of those
((p 'bar)
(q p)) ; which p do you want? The first or the second?
(list p q)))
Try it:
CL-USER 60 > (let ((p 'foo))
(let ((p 'bar)
(q p))
(list p q)))
(BAR FOO)
CL-USER 61 > (let ((p 'foo))
(let* ((p 'bar)
(q p))
(list p q)))
(BAR BAR)
Note also that there is an indirect dependency in LET
: the values for the bindings are evaluated left to right:
(let ((list '(1 2)))
(let ((a (pop list))
(b (pop list)))
(list a b)))
The result of above form will always be (1 2)
.
Upvotes: 5