Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

difference between &rest and &body

1,758 views
Skip to first unread message

Duane Smith

unread,
Apr 23, 2000, 3:00:00 AM4/23/00
to
Hi-

I am new to Lisp and am wondering what is the difference between using
&rest and &body in a macro definition. They appear to do the same
thing.

Thanks in advance,
nick...@ptcstudios.com

Tor Henrik Hanken

unread,
Apr 23, 2000, 3:00:00 AM4/23/00
to
[Duane Smith]

| I am new to Lisp and am wondering what is the difference between using
| &rest and &body in a macro definition. They appear to do the same
| thing.

&body gives smart editors and code formatters a hint about how to
indent the code. apart from that, they do exactly the same thing.
--
mvh

Tor Henrik Hanken

Tim Bradshaw

unread,
Apr 23, 2000, 3:00:00 AM4/23/00
to
* Duane Smith wrote:
> Hi-

> I am new to Lisp and am wondering what is the difference between using
> &rest and &body in a macro definition. They appear to do the same
> thing.

I think the idea is that &BODY is a hint to the system and the human
reader of the code that the remaining arguments are something like a
function body (I forget if there is a formal name for that), and thus
is should, for instance, be indented like a function body. Since this
is quite a common form for macros, it seemed like a worthwhile thing
to do.

Unfortunately none of the emacs-based lisp environments I'm familiar
with look closely enough at things like macro definitions to spot this
case. However, here is what Genera does with two otherwise-identical
definitions:

;;; -*- Mode: LISP; Base: 10; Syntax: Ansi-common-lisp; Package: CL-USER -*-

(defmacro with-grible ((x) &rest body)
`(let ((grible ,x)) ,@body))

(with-grible (1)
grible)

(defmacro with-better-grible ((x) &body body)
`(let ((grible ,x)) ,@body))

(with-better-grible (1)
grible)

-- so one of the environments that was very influential when CL was
first being formalised did this.

(I wish some heroic emacs person would get emacs to spot this, and
also allow it to scan the buffer looking for something like the
zwei:defindentation macro / zwei:indentation declaration, or at least
its simple case).

--tim

Tom Breton

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
Duane Smith <nick...@ptcstudios.com> writes:

> Hi-
>
> I am new to Lisp and am wondering what is the difference between using
> &rest and &body in a macro definition. They appear to do the same
> thing.

Yes, functionally, they do exactly the same thing. &body is a sort of
hint to editors that the arguments after it are in an implicit progn,
for formatting purposes mostly.

--
Tom Breton, http://world.std.com/~tob
Not using "gh" since 1997. http://world.std.com/~tob/ugh-free.html
Rethink some Lisp features, http://world.std.com/~tob/rethink-lisp/index.html
Some vocal people in cll make frequent, hasty personal attacks, but if
you killfile them cll becomes usable.

Erik Naggum

unread,
Apr 24, 2000, 3:00:00 AM4/24/00
to
* Duane Smith <nick...@ptcstudios.com>

| I am new to Lisp and am wondering what is the difference between using
| &rest and &body in a macro definition. They appear to do the same thing.

yes, they _do_ the same thing, but they _mean_ different things. the
same is true of car and first, for instance. remember that software is
not written for the computer, but for programmers, including yourself.
the more precise you are in communicating your _intent_ when writing, the
less you have to worry about reconstructing your intent when you read it
later. &rest mainly communicates "a list of objects, mostly of similar
type", while &body mainly communicates "forms to be evaluated in the
context set up by the macro". there are lots of variations on this
theme, of course, but you get the idea.

#:Erik

Will Mengarini

unread,
May 14, 2000, 3:00:00 AM5/14/00
to
Tim Bradshaw <t...@cley.com> 23 Apr 2000 21:48:26 +0100:

> [...] &BODY is a hint to the system and the human reader of the


> code that the remaining arguments are something like a function

> body [...]. Unfortunately none of the emacs-based lisp


> environments I'm familiar with look closely enough at things

> like macro definitions to spot this case. [...] (I wish some
> heroic emacs person would get emacs to spot this [...])

Emacs Lisp built-in #'defmacro etc don't even recognize &body, but
cl-macs.el defines #'defmacro* etc that do recognize it (along with
almost all the rest of Common Lisp's lambda list capabilities), so you
have to use the splatted versions. Even those, however, don't
recognize the prettyprint significance of &body, and this fixes that:

(defadvice cl-transform-lambda (after wm-cl-&body act)
"Recognize &body as changing the prettyprint."
(when (and (memq '&body (car form))
;; The argument list for the lambda form contained
;; '&body, so the symbol being bound should be
;; given defun-like indentation. Such actions are
;; carried out in a block of code returned by
;; `cl-transform-lambda' as the car of its returned
;; value; the cdr is the emacsified lambda.
;; In order for this to be possible, the
;; lambda being transformed needs to be being
;; bound to a name. If it is, the symbol was
;; passed in as the arg named `bind-block':
(not (eq bind-block 'cl-none)))
(let ((additional-action `(put ',bind-block
'lisp-indent-function
'defun)))
(if (car ad-return-value)
;; There's already an actions block there. Require it to be
;; of recognizable form; if it's not, degrade gracefully by
;; (message)ing a warning but then doing nothing.
(if (and (eq (caar ad-return-value) 'eval-when)
(equal (cadar ad-return-value) '(compile load eval)))
(push additional-action (cddar ad-return-value))
(message "wm-cl-&body: does not compute: %S"
(car ad-return-value)))
;; There's no actions block yet, so just replace the nil.
(setf (car ad-return-value)
`(eval-when (compile load eval)
,additional-action))))))

> ([...] and also allow it to scan the buffer looking for


> something like the zwei:defindentation macro / zwei:indentation

> declaration, or at least its simple case.)

What are these? Deja only found your post to which I'm replying,
and Google mostly came up dry, except for
http://www.google.com/search?q=zwei:indentation&sa=Google+Search
which found 2 pages in Sweden that render as line noise.

Will Mengarini <sel...@eskimo.com>
Free software: the Source will be with you, always.

Barry Margolin

unread,
May 15, 2000, 3:00:00 AM5/15/00
to
In article <8fm20k$n4a$1...@eskinews.eskimo.com>,

Will Mengarini <sel...@eskimo.com> wrote:
>Tim Bradshaw <t...@cley.com> 23 Apr 2000 21:48:26 +0100:
>> ([...] and also allow it to scan the buffer looking for
>> something like the zwei:defindentation macro / zwei:indentation
>> declaration, or at least its simple case.)
>
>What are these? Deja only found your post to which I'm replying,
>and Google mostly came up dry, except for
>http://www.google.com/search?q=zwei:indentation&sa=Google+Search
>which found 2 pages in Sweden that render as line noise.

ZWEI is the Emacs-style editor on Lisp Machines, and those are the
functions/macros that are used to tell ZWEI how it should indent various
forms.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

0 new messages