Church numerals
This page is the answer to the task Church numerals in the Rosetta Code.
Contents
 1 Description (from Rosetta Code)
 2 Answers
 2.1 Church numeral zero
 2.2 The succesor function
 2.3 Subsecuent Church numerals from the Church numeral zero and the succesor function
 2.4 The sum function
 2.5 The product function
 2.6 The exponentiation function
 2.7 Conversion from Church numerals to integers
 2.8 Conversion from integers to Church numerals
 2.9 Results as integers, operands given as Church numerals
 2.10 Results as integers, operands converted from integers
Description (from Rosetta Code)
Task
In the Church encoding of natural numbers, the number N is encoded by a function that applies its first argument N times to its second argument.
Arithmetic operations on natural numbers can be similarly represented as functions on Church numerals. In your language define:
You should:

Answers
Church numeral zero
By definition:
The succesor function
The succesor function is defined as:
The orange mapping symbol is a lambda expression builder: It creates a lambda expression where its components (variables, body) are calculated from other expressions.
Subsecuent Church numerals from the Church numeral zero and the succesor function
The sum function
The sum function is defined as:
Example. Adding the numeral 3 and 4:
The product function
The product function is defined as:
Example. Multiplying the numeral 3 and 4:
The exponentiation function
The exponentiation function is defined as:
Example. Powering the numerals 4^{3} and 3^{4}:
Conversion from Church numerals to integers
It is achieved giving an incrementer as function, and a value of zero to a given Church numeral:
Example:
Conversion from integers to Church numerals
It is achieved calling recursively the succesor function from a Church numeral zero.
Example:
Results as integers, operands given as Church numerals
It calculates 3 + 4, 3 × 4, 4^{3} and 3^{4}:
Results as integers, operands converted from integers
It calculates 3 + 4, 3 × 4, 4^{3} and 3^{4}: