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.
In article <1050b059-5ae2-4224-af2d-a791fbf81...@n18g2000vbv.googlegroups.com>,
John Cowan <johnwco...@gmail.com> wrote:
> 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.
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 ***
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!