The rational for returning the identity for expressions such as (*) and (+)

This post in comp.lang.scheme brings up a question that inevitably everyone asks.
Suppose the answer is obvious but John summed it up well:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *