all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* About `defun' in elisp manual
@ 2012-12-28  7:50 Xue Fuqiao
  2012-12-28 14:49 ` Florian v. Savigny
       [not found] ` <mailman.16211.1356706167.855.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Xue Fuqiao @ 2012-12-28  7:50 UTC (permalink / raw)
  To: help-gnu-emacs

The GNU Emacs Lisp Reference Manual(http://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Functions.html#Defining-Functions) says:
     `defun' is the usual way to define new Lisp functions.  It defines the symbol NAME as a function that looks like this:
          (lambda ARGUMENT-LIST . BODY-FORMS)

Why is the lambda expression a dotted list?  As far as I'm concerned, a normal list is enough.

BTW, it also says:
 -- Special Form: defun name argument-list body-forms...
It doesn't mention the [DOCSTRING] argument, is it a bug?
-- 
Best regards.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: About `defun' in elisp manual
       [not found] <mailman.16203.1356681053.855.help-gnu-emacs@gnu.org>
@ 2012-12-28 11:07 ` Pascal J. Bourguignon
  2012-12-28 15:35   ` Drew Adams
  0 siblings, 1 reply; 6+ messages in thread
From: Pascal J. Bourguignon @ 2012-12-28 11:07 UTC (permalink / raw)
  To: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

> The GNU Emacs Lisp Reference Manual(http://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Functions.html#Defining-Functions) says:
>      `defun' is the usual way to define new Lisp functions.  It defines the symbol NAME as a function that looks like this:
>           (lambda ARGUMENT-LIST . BODY-FORMS)
>
> Why is the lambda expression a dotted list?  As far as I'm concerned, a normal list is enough.

It is not.


All lists are made of conses, and conses are written (a . d)

A proper list is nil or a cons cell where d is a proper list.

A dotted list is a non-nil atom or a cons cell where d is a dotted list.

A circular list is a circular list or a cons cell where d is a circular
list.   Oops, that definition is circular.  A circular list is a list
where one of the cdr of the cons cells is one of the previous cons
cells.  (But not necessarily the first).



Above, BODY-FORM must be a proper list therefore the lambda expression
is a proper list.



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: About `defun' in elisp manual
  2012-12-28  7:50 Xue Fuqiao
@ 2012-12-28 14:49 ` Florian v. Savigny
       [not found] ` <mailman.16211.1356706167.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Florian v. Savigny @ 2012-12-28 14:49 UTC (permalink / raw)
  To: Xue Fuqiao; +Cc: help-gnu-emacs


I'm not an expert, but it would seem to me that by "dotted list", you
mean a cons cell, and if BODY-FORMS is a list, the two are the
same. Try evaluating the following expression in your *scratch*
buffer:

'(foo . (bar . (fubar . (baz))))

You should get a simple list of the four elements. Elisp builds lists
out of cons cells, which is explained in the Elisp Manual, Section
5.1. (However, there is no more ASCII art, which I think is a shame.)

This is also the reason why the following expression are equivalent:

(nth 0 my-list)
(car my-list)

(I'm not sure why Elisp builds lists that way, since this is not too
intuitive, but I suspect it has something to do with the very limited
technological possibilities at the time when Emacs was first written.)

I hope I'm not completely wrong, and that this sheds some light on the
matter. (Otherwise, I'll keep quiet in the future.)

Best regards,

Florian




^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: About `defun' in elisp manual
  2012-12-28 11:07 ` About `defun' in elisp manual Pascal J. Bourguignon
@ 2012-12-28 15:35   ` Drew Adams
  2012-12-28 16:15     ` Drew Adams
  0 siblings, 1 reply; 6+ messages in thread
From: Drew Adams @ 2012-12-28 15:35 UTC (permalink / raw)
  To: 'Pascal J. Bourguignon', help-gnu-emacs

> Above, BODY-FORM must be a proper list therefore the lambda expression
> is a proper list.

While correct and providing the info needed for understanding, I'm not sure that
the explanation given so far will directly bridge Fuqiao's misunderstanding.

The first thing is the typo: it is BODY-FORMS, not BODY-FORM, and that is an
important clue.

The point is that a lambda expression does not *require one* sexp (a list or an
atom) after ARGUMENT-LIST - it accepts any number of sexps, including zero.

BODY-FORMS is a list here - a list of body forms, which themselves are often
lists.  If a lambda expression accepted only one list as its body then removing
the dot for the syntax would be correct.

BODY-FORMS has zero or more elements.  By using the dot, the syntax indicates
that the list is *spliced into* the lambda expression, instead of simply
appearing as a single list sexp.

IOW, each of its elements is a body form, and they are what appear directly in
the lambda expression.  If BODY-FORMS is ((foo bar)) then there is a single body
form, (foo bar), and the sexp is (lambda ARGUMENT-LIST (foo bar)).  If
BODY-FORMS is (foo bar) then there are two body forms, foo and bar, and the sexp
is (lambda ARGUMENT-LIST foo bar).

Now go back and read what Pascal and Florian wrote, explaining that lists are
really dotted lists.

As Pascal said, BODY-FORMS is a *proper* list, that is, a dotted list with () as
its final cdr.

If BODY-FORMS is (X Y Z) then (A . BODY-FORM) is (A X Y Z), that is,
(A . (X . (Y . (Z . nil))))


[Be aware that sometimes the Emacs doc writes "BODY-FORMS..." instead of ".
BODY-FORMS".  And it might not be clear to some readers whether that means one
or more or zero or more sexps.  In most contexts other than Emacs, "A..." stands
for a single A followed possibly by additional As: one or more repetitions of A.

But in Emacs doc the "..." seems to stand for zero or more, not one or more, and
the Emacs doc writes "BODY-FORMS..." (plural and with repetitions), not
"BODY-FORM...".  Sometimes it writes "BODY..." (singular) and then in the text
refers to BODY in the plural: "forms".  (Also, sometimes there is a space before
"...", sometimes not.)

The syntax used in the description of a lambda expression is simpler and has no
such ambiguity and sending of mixed signals - it is Lisp syntax!]




^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: About `defun' in elisp manual
  2012-12-28 15:35   ` Drew Adams
@ 2012-12-28 16:15     ` Drew Adams
  0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2012-12-28 16:15 UTC (permalink / raw)
  To: 'Drew Adams', 'Pascal J. Bourguignon',
	help-gnu-emacs

> If BODY-FORMS is (X Y Z) then (A . BODY-FORM) is (A X Y Z), that is,
                                     ^^^^^^^^^
                                     BODY-FORMS
> (A . (X . (Y . (Z . nil))))




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: About `defun' in elisp manual
       [not found] ` <mailman.16211.1356706167.855.help-gnu-emacs@gnu.org>
@ 2012-12-28 17:52   ` Pascal J. Bourguignon
  0 siblings, 0 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2012-12-28 17:52 UTC (permalink / raw)
  To: help-gnu-emacs

florian@fsavigny.de (Florian v. Savigny) writes:

> I'm not an expert,

Yes, and you won't be anytime soon if you go on like this:

> but it would seem to me that by "dotted list", you
> mean a cons cell, 

That's not the definition of a dotted list.  I gave the definitions!
Just read them and learn them.  If you don't trust me, just take any
good lisp book (eg. the Common Lisp HyperSpec glossary) for their
definitions!

> and if BODY-FORMS is a list, the two are the
> same. 

No. That would depend on the kind of list BODY-FORMS is.

> (I'm not sure why Elisp builds lists that way, since this is not too
> intuitive, 

Because that's what lists are! DUH!

Lists are singly linked chains of cons cells.

Notably, lists ARE NOT vectors! (Like some other programming languages
want you to believe).

The difference between the various lists, is how they terminate or not.


> but I suspect it has something to do with the very limited
> technological possibilities at the time when Emacs was first written.)

It's not a technological limitation whatsoever.  The 7090 or the
processors where GNU emacs was written are as good as the latest Intel
processors (that reflects badly on those later…).



> I hope I'm not completely wrong, and that this sheds some light on the
> matter. (Otherwise, I'll keep quiet in the future.)

Try to learn something first.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-12-28 17:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.16203.1356681053.855.help-gnu-emacs@gnu.org>
2012-12-28 11:07 ` About `defun' in elisp manual Pascal J. Bourguignon
2012-12-28 15:35   ` Drew Adams
2012-12-28 16:15     ` Drew Adams
2012-12-28  7:50 Xue Fuqiao
2012-12-28 14:49 ` Florian v. Savigny
     [not found] ` <mailman.16211.1356706167.855.help-gnu-emacs@gnu.org>
2012-12-28 17:52   ` Pascal J. Bourguignon

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.