As probably all experienced elispers have found at some point, code like is broken:
(let ((a 3)
(b 4)
(c (+ a b)))
c)
One should use the let*
form instead when referring to a just-binded variable within the binding clauses.
I just wonder - why is a seemingly wrong behavior the default? Are there any risks on choosing always let*
regardless of how is one gonna use it?
Any let*
forms can be easily (and mechanically) expressed using let
. For example, if we didn't have the let*
special form, we could express
(let* ((a 3)
(b 4)
(c (+ a b)))
c)
as:
(let ((a 3))
(let ((b 4))
(let ((c (+ a b)))
c)))
On the other hand, some let
forms are quite difficult, if possible at all, to express using let*
. The following let
form cannot be expressed using let*
without introducing an additional temporary variable or esoteric bitwise manipulation.
(let ((x y) (y x)) ; swap x and y
...)
So, in this sense, I think let
is more fundamental than let*
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With