8

I've seen both (let (var) ...) and (let ((var nil)) ...) is there any difference between these statements?

2 Answers 2

13

The documentation for let says:

Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).

The let (var) variant matches the first line — var is a symbol, bound to nil. The let ((var nil)) variant matches the second line — (var nil) where var is the symbol and the initial value is nil.

They do the same thing in this case.

13

@amitp provided the answer. They do have the same behavior.


However, IMO they can indicate something slightly different to a human reader of the code -- at least according to an informal convention. That is, they can convey a different connotation.

I use (let (foo) ...) only when the initial value is intentionally set in the let body, e.g., in a conditional way. It tells me, as a (later) reader of my own code, that an initial value of nil, which is what it provides, isn't used - makes no difference.

I use let ((foo nil)) ...) to indicate that the nil binding matters -- it really is an intentional initialization. I do it to make the value more obvious.

I do the latter also in the case of binding a global variable for which I know that a nil value has a particular behavior. Using that form, with the explicit nil, points out to me clearly that I'm imposing that nil behavior there. IOW, it just makes the binding more obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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