9

I've used let before, but never used let* what does let* ( let asterisk) mean in elisp?

fyi this one was hard to google because of the asterisk.

15

It means that when there is more than one binding the variables are bound sequentially, so that each can depend on the values bound to those that are bound before it, i.e., those that come before it in the let* bindings.

For let (no asterisk), none of the bindings can be assumed to occur before any of the others (the variables could, in principle, be bound in parallel).

So (let ((a 42) (b a))...b) can raise an error because it tries to bind b to the value of a, and a (at least the particular a that is bound in this let) cannot be assumed to have been bound to a value.

[If some other (e.g. global) variable a is bound to a value, e.g., 5, then that let expression would bind its (local) a to 42 and bind b to the value of the global a, i.e., 5.]

But (let* ((a 42) (b a))...b) returns 42 because a gets bound to 42 and then b gets bound to the value of a.

See the Elisp manual, node Local Variables.


Update, from @DoMiNeLa10's reminder in a comment:

In addition to the manual, you can also use the help commands. Of all the various ways to "ask Emacs", they are among the first to learn and the most useful.

And for C-h f let* tells us, succinctly: Each VALUEFORM can refer to the symbols already bound by this VARLIST.


Though there's nothing wrong with asking here, you would do yourself a favor if you asked Emacs such questions. Just use C-h i to open the Info browser of manuals, choose the Elisp manual, and use i let* to look up let* in the index (with completion). That takes you directly to the node that covers this.

(And the manual typically covers things better than will be done here, as a lot of time and effort, by thousands of contributors, have gone into getting it right: correct and complete.)


You can also search this site for questions and answers that use tag let-binding: Just put [let-binding] in the search field. (I'll do that now, to see if this question is perhaps a duplicate...)

  • there is a typo in the above example, when b equals 5 because it is bound to global a. It should read let instead of let* – jue Jul 7 '18 at 7:02
  • @jue: Right; thx. – Drew Jul 7 '18 at 14:50
  • It's also possible to ask about functions with F1 f. – DoMiNeLa10 Jul 7 '18 at 15:30
  • @DoMiNeLa10: Indeed; good point. As comments can be deleted anytime I've updated the answer to mention this. Thx. – Drew Jul 8 '18 at 0:19
3

let* is like a recursive let meaning:

(let* ((var1 VAL1)
       (var2 VAL2))
  ...)

is like

(let ((var1 VAL1))
  (let ((var2 VAL2))
    ...))

Thereby the usage of capital letters for VAL1 and VAL2 indicates that those can also be expressions.

The recursive let-binding has consequences if VAL2 depends actually on var1. The effect is already described in Drew's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.