I vaguely remember someone (Norvig, Graham?) haven't written something
(passed in a book, essay, etc.) about how he will frequently jot down a
snippet of lisp code, for example, on a sheet of paper, without any
parentheses, just the indentation, adding all the parentheses when he is
actually sitting at an editor. Or am I just conflating snippets of
something I read on usenet?
> I vaguely remember someone (Norvig, Graham?) haven't written something
> (passed in a book, essay, etc.) about how he will frequently jot down a
> snippet of lisp code, for example, on a sheet of paper, without any
> parentheses, just the indentation, adding all the parentheses when he is
> actually sitting at an editor. Or am I just conflating snippets of
> something I read on usenet?
The following text is from Ansi Common Lisp, by Paul Graham, page 18:
But when the code is properly indented, one has no trouble. You could
omit most of the parentheses and still read it:
defun our-member (obj lst)
if null lst
nil
if eql (car lst) obj
lst
our-member obj (cdr lst)
Indeed, this is a practical approach when you're writing code on paper.
Later, when you type it in, you can take advantage of paren-matching in
the editor.
Eric Lavigne wrote:
>> I vaguely remember someone [...] haven't written something
>> [...] about how he will frequently jot down a
>> snippet of lisp code, for example, on a sheet of paper, without any
>> parentheses, just the indentation, [...]
>
> The following text is from Ansi Common Lisp, by Paul Graham, page 18:
That's exactly what I was trying to remember. Thanks.
Damien Kick <·····@earthlink.net> writes:
> I vaguely remember someone (Norvig, Graham?) haven't written something
> (passed in a book, essay, etc.) about how he will frequently jot down
> a snippet of lisp code, for example, on a sheet of paper, without any
> parentheses, just the indentation, adding all the parentheses when he
> is actually sitting at an editor. Or am I just conflating snippets of
> something I read on usenet?
There are two reasons why the parentheses are needed:
1- We want to be able to write forms with variable arity:
(+ 1 2 3 4 5) (progn (print a) (print b) (print c))
instead of:
+ + + + 1 2 3 4 5 (seq (seq (print a) (print b)) (print c))
or:
+ 1 + 2 + 3 + 4 5 (seq (print a) (seq (print b) (print c)))
We could do with a terminator:
+ 1 2 3 4 5 ] progn print a ] print b ] print c ] ]
2- Both for programs (compilers, code-walkers, editors, etc) and for
human programmers, even for operators of fixed arity it's better
to denote the arity because it is not necessarily known by the reader:
title get-verse chapter chapter-number chapter
What is the structure of this form? Every operator present here
are of fixed arity. You should be able to indent this form
properly. You should be able to collect the list of functions
called from this form. You should even be able to COMPILE it!
But not until you know the arity of each operator.
If we have title/2 get-verse/2:
(title (get-verse chapter chapter-number) chapter) ; 1
If we have title/2 get-verse/2 chapter-number/0
(title (get-verse chapter (chapter-number) chapter) ; 2
If we have title/2 get-verse/1 chapter/1
(title (get-verse (chapter chapter-number)) chapter) ; 3
If we have title/1 get-verse/2 chapter-number/1:
(title (get-verse chapter (chapter-number chapter))) ; 4
We couldn't use a terminator anymore:
title get-verse chapter chapter-number ] chapter ] ; 1
title get-verse chapter chapter-number ] ] chapter ] ; 2
title get-verse chapter chapter-number ] ] chapter ] ; 3
title get-verse chapter chapter-number chapter ] ] ] ; 4
Forms 2 and 3 would be identical.
However, these are global effects. When you write CAR, you know that
you need exactly one argument, so if you write CAR X the editor could
notice that X is not a function and insert the parentheses for you.
I've got experimental emacs code do to that. Since it needs to know
the arity of the operators, for now it works automatically only for
COMMON-LISP operators. It would need to be integrated with slime for
example to fetch the arity of user defined operators.
See pjb-cl-magic.el in:
cvs -z3 -d \
··················@cvs.informatimago.com:/usr/local/cvs/public/chrooted-cvs/cvs \
co emacs
--
__Pascal Bourguignon__ http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"