Common Lisp: is a simple variable a shorthand for a list in LET?

I read Peter Seibel's Practical General Lisp of Lisp, and I came across the following expression in the DO section of the Macros chapter :

As in the case of defining variables in LET, if the init form is not taken into account, the variable is bound to NIL. In addition, as with LET, you can use the usual variable name as an abbreviation for a list containing only the name.

I do not know what it means "using a simple variable as shortand for a list containing only a name", that is, the second sentence. The first sentence is reinforced by Common-Lisp Hyperspec for LET and a simple example like (let (n) n) ~~> NIL , but I can’t find anything that matches the second sentence. Actually, something like (let nn) creates an SBCL compiler.

What does it mean? What is a minimal example of this use?

+6
source share
1 answer

What does it mean?

It just means that all three of them have the same effect:

 (let ((x nil)) (let ((x)) (let (x) x) x) x) 

In each case, x is bound to nil . Most people are familiar with the first thing. The second case does not include the init form, but Common Lisp is defined to bind x to nil in this case. Of course, the second case, one way to look at it, has more brackets that you need (it's just an extra set of parentheses around the variable), so you can even take another shortcut and just write the variable yourself.

Where is he listed?

In the documentation for let, we see that let syntax is:

let ({var | (var [init-form])} *) Declaration * form * & Rightarrow; result *

From this, we see that each use of let will look something like this:

 (let (…) …) 

But what is included in this internal list?

{var | (var [init-form])} *

* means that in this list there can be any number (zero or more), and each of them either corresponds to var, or (var [init-form]). Var is just a character that can be used as a variable. (Var [init-form]) is a list in which var is used as the first element, and optionally has a second element - init-form.

But this means that in two possible cases (var var separately, and a list without an init form) there is no init-form. Instead of having an unbound or uninitialized variable, Common Lisp defines nil in these cases.

Why is so much allowed? Many of these consist in consistency between the various special forms in Common Lisp. Take a look at the VARIABLE-LIST-ASYMMETRY Writeup Edition . For more information on reading syntax specifications in the documentation, see 1.4.4.20 Section "Syntax" of the dictionary.

+14
source

Source: https://habr.com/ru/post/975244/


All Articles