Church numbers (retired)
Description:
We are going to improve the defition of the natural numbers used in this kata.
You should complete the previous kata before you try this one.
We will use the following constructor functions of natural numbers which are more in line with the definition of Church numbers (in the examples I am going to explain the f
and x
parameters).
function zero(f, x) {
return x;
}
function succ(church) {
return function(f, x) {
return f(church(f, x));
};
}
So, we can define natural numbers in this way:
0 -> zero
1 -> succ(zero)
2 -> succ(succ(zero))
...
To construct a natural number from a JavaScript integer parameter, we can use this function:
function intToChurch(int) {
var church = zero;
for (var i = 1; i <= int; i++) {
church = succ(church);
}
return church;
}
So:
zero -> intToChurch(0)
one -> intToChurch(1)
nine -> intToChurch(9)
To perform the complementary operation, you can use this function:
function churchToInt(church) {
return church(sumOne, 0);
}
var sumOne = sum.bind(null, 1);
function sum (x, y) {
return x + y;
}
For example:
churchToInt(succ(succ(zero))) === 2;
I will explain how this works:
churchToInt(zero) -> zero(sumOne, 0) -> 0
churchToInt(succ(zero)) -> succ(zero)(sumOne, 0) -> sumOne(zero(sumOne, 0)) -> sumOne(0) -> 1
churchToInt(succ(succ(zero))) -> succ(succ(zero))(sumOne, 0) -> sumOne(succ(zero)(sumOne, 0)) -> sumOne(1) -> 2
That is, the recursive definition of the function succ
allows us to define very simple functions on it, and they do not require loops nor recursive processes. Furthermore, the implementation does not require conditions. Compare the implementations of intToChurch
and curchToInt
.
In the execution of the succ
function three parameters are involved:
1.- church
: Defines the number of times to execute the function f
.
2.- f
: Is the function to be executed.
3.- x
: Is the initial value returned when the number is zero
and the parameter passed to the function the first time this is executed. The parameter passed to the function will be progressively transformed with each execution.
Your work is implement the next functions:
add(church, church) -> church // performs the addition of two church numbers
add(zero, zero) -> zero
add(zero, succ(zero)) -> succ(zero)
add(succ(zero), zero) -> succ(zero)
add(succ(zero), succ(zero)) -> succ(succ(zero))
mul(church, church) -> church // performs the multiplication of two church numbers
mul(zero, zero) -> zero
mul(zero, succ(zero)) -> zero
mul(succ(zero), zero) -> zero
mul(succ(zero), succ(zero)) -> succ(zero)
mul(succ(succ(zero)), succ(zero)) -> succ(succ(zero))
pow(church, church) -> church // raises the first church parameter to the power of the second church parameter
pow(succ(zero), succ(zero)) -> succ(zero)
pow(succ(zero), zero) -> succ(zero)
pow(succ(zero), succ(succ(zero))) -> succ(zero)
pow(succ(succ(zero)), succ(zero)) -> succ(succ(zero))
pow(succ(succ(zero)), succ(succ(zero))) -> succ(succ(succ(succ(zero))))
Similar Kata:
Stats:
Created | Nov 21, 2014 |
Warriors Trained | 1017 |
Total Skips | 252 |
Total Code Submissions | 5516 |
Total Times Completed | 178 |
JavaScript Completions | 178 |
Total Stars | 47 |
% of votes with a positive feedback rating | 91% of 68 |
Total "Very Satisfied" Votes | 60 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 4 |
Total Rank Assessments | 9 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |