[Tutor] church numerals (fwd)
Alan Gauld
alan.gauld at btinternet.com
Sun Nov 12 23:19:57 CET 2006
> From: Asrarahmed Kadri <ajkadri at googlemail.com>
>> #########################
>>>>> def iszero(n):
>> ... def s(x):
>> ... return False
>> ... return n(s, True)
>
>
> Where is n() defined ...?????
n is a parameter of the function.
Thus to call iszero you need to pass in another function
which in turn takes a function and a boolean as its
arguments.
Going back to Danny's original example:
>>> def zero(s, z):
... return z
...
>>> def one(s, z):
... return s(z)
Both of these "numbers" match the type of fuction that
iszero expects. So we can do:
iszero(zero)
Now n takes on the vale of zero
and the return line of iszero becomes:
return zero(s,True)
and zero returns its second argument which is True,
so zero is zero, as expected.
For iszero(one)
the return line is
return one(s,True)
but inside one the return is now
return s(True), but the return value of s is False.
So the ultimate reurtn value of iszero(one) is False, again
as expected.
This is fairly mind bending the first time you come across it
so don;t panic,ust work your way through a few more examples
as I did above.
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list