Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't let* the default let?

Tags:

emacs

lisp

elisp

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?

like image 229
deprecated Avatar asked Nov 28 '22 03:11

deprecated


1 Answers

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*.

like image 198
dkim Avatar answered Mar 07 '23 13:03

dkim