# identity

28 views

### time

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

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

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

Nov 6, 2011, 1:07:49 AM11/6/11
to
In article
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 ***