3 ms·
Lisp syntax is two stage: at the first level it is a data syntax. It provides via s-expressions a textual representation for data. It also helps to format data
by lispm 9y ago
Lisp syntax is two stage: at the first level it is a data syntax. It provides via s-expressions a textual representation for data. It also helps to format data to make it more readable to a human reader:
CL-USER 45 > (pprint '(foo bar (alpha beta gamma) (delta alpha beta gamma)))
(FOO
BAR
(ALPHA BETA GAMMA)
(DELTA ALPHA BETA GAMMA))
but the tabular print may help with reading. S-expressions know nothing about Lisp syntax. All they do is describe how to textual represent data like lists, symbols, numbers, strings, arrays, ...
At the second stage is Lisp program syntax. It is defined on top of s-expressions.
This is the syntax for DEFUN, which defines functions:
defun function-name lambda-list [[declaration* | documentation]] form*
A function name, the lambda-list, declacations and forms have then more syntax.
If we have a defun form, we also want it formatted.
I take the Lisp form above, and change the first identifier to defun so that it is valid Lisp code:
CL-USER 46 > (pprint '(defun bar (alpha beta gamma) (delta alpha beta gamma)))
(DEFUN BAR (ALPHA BETA GAMMA)
(DELTA ALPHA BETA GAMMA))
Now you see that the printer recognizes that DEFUN is a valid Lisp macro and formats it this way:
DEFUN function-name argument-list
form-1
..
form-n
It does format it differently from the similar structured list above. As a Lisp programmer I expect the second formatting for valid Lisp code.
The Lisp programmer uses the s-expression syntax as a clue, but actually follows the special formatting/indenting of Lisp code to read it. The experienced programmer doesn't see the parentheses, but follows the head symbols (here DEFUN), the special grouping, the blocks, the indentation. As a Lisp programmer I know that after the name the argument list follows and then optional type declarations and documentation string. After that I expect the body forms as a sequence of forms.