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

identity

28 views
Skip to first unread message

time

unread,
Nov 5, 2011, 5:45:21 PM11/5/11
to
What is the rational for returning the identity for expressions such as (*) and (+)?

Thanks!

Pascal Costanza

unread,
Nov 5, 2011, 7:42:29 PM11/5/11
to
On 05/11/2011 22:45, time wrote:
> What is the rational for returning the identity for expressions such as (*) and (+)?

...to ensure the operators work well with apply.

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
The views expressed are my own, and not those of my employer.

John Cowan

unread,
Nov 5, 2011, 7:52:43 PM11/5/11
to John Cowan
On Nov 5, 5:45 pm, time <timreve...@gmail.com> wrote:

> What is the rationale for returning the identity for expressions such as (*) and (+)?

Well, if you interpret (apply + some-list) and (apply * some-other-
list) as left-folds, then a starting point is needed, something to
begin accumulating from. The identity is the only value that works
correctly, so it's what gets returned when the list being folded over
has length 0. Nobody would write (+) or (*) explicitly, but having
them return the identity is the Right Thing nevertheless.

The same argument applies when expanding macros: in a degenerate case,
a macro may expand to (+) or (*), and it's usually not appropriate to
raise an exception.

Barry Margolin

unread,
Nov 6, 2011, 1:07:49 AM11/6/11
to
In article
<1050b059-5ae2-4224...@n18g2000vbv.googlegroups.com>,
Another way to explain it is that it allows the associative property to
include the limiting case of an empty argument list:

(+ a b c d e f) is obviously equivalent to any of the following:

(+ (+ a b c d e) (+ f))
(+ (+ a b c d) (+ e f))
(+ (+ a b c) (+ d e f))
...
(+ (+ a) (+ b c d e f))

When you define the empty cases as returning the identity, it also
includes:

(+ (+) (+ a b c d e f))

This then makes the right thing happen for apply.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

time

unread,
Nov 15, 2011, 4:28:24 PM11/15/11
to
Hey, thanks! At this point I'm assuming that this sort of thing can be discussed in terms of Lambda calculus ... I've just started Church's book -- which is more readable than some of his commentators! And Church seems to hook up nicely with thinking about logic and linguistics. All of which what makes Scheme such an interesting language!
0 new messages