Next: zero Up: Natural Numbers Previous: Natural Numbers   Contents   Start

### Explanation of `three apples'

Now it should not be difficult to understand the code that lies behind the expression `(three apples)'.
• The function `three' expects one argument symbolized by `f' in it's definition. `f' is supposed to be a function taking one argument symbolized by `x'.
• The abstraction `apples' is a function taking one argument and can therefore be synthesized with `three', or in other words `three' can be invoked with `apples' as an argument.
• The result of this synthesis or function call is the no-name function taking one argument symbolized by `x'.
• In other words our original expression `(three apples)' would not return to us our desired apples but another abstraction instead that has to be synthesized with a basket (symbolized by `x') before we get the basket filled with three apples (if the basket was empty at the beginning).
• We get three apples because the function `f' was invoked three times with the basket as an argument as returned from the previous function call.
• To receive our desired apples finally we have to change our original expression into the following one:
• We would like to leave it up to readers to explain to themselves why the following expression would not be correct and therefore would not result in basket filled with apples:
As clumsy as this example may be, it nevertheless should have made clear how numbers are defined in A++, following the idea of Alonzo Church in his Lambda Calculus. Numbers of this kind are called `Church Numerals'.

As interesting as these 'Church Numerals' are, readers might still ask the question `Why bother with them?' if they are not needed later in any practical programming work (Java, C, C++, not even Lisp and Scheme).

From the educational viewpoint these Church Numerals are extremely important however, because they make us very quickly familiar with the core of functional programming: functions have the same status as data,.

• they can be passed as arguments,
• they can be returned by functions, and
• new functions can be created on the fly.
This is what people mean, when they use the phrase: functions are first class objects or functions have the status first class.

Following the outlined approach above we can define the following abstractions:

Next: zero Up: Natural Numbers Previous: Natural Numbers   Contents   Start

Georg P. Loczewski 2004-03-05