How to get predecessor of a church numeral
I'm practicing with SML and I'm doing a small assignment where we have to implement Church numerals defined as:
datatype 'a 开发者_如何学编程church = C of ('a -> 'a) * 'a -> 'a
example val
ZERO = C(fn (f,x) => x)
I have already implemented the functions:
create: int -> 'a church
churchToInt: 'a church -> int
and SUC
which returns the successor of a Church numeral.
Now I have to implement the function
PRED: 'a church -> 'a church * 'a church
which returns the tuple of (predecessor, current numeral). I am not allowed to use churchToInt
, I should directly work with Church numerals. Apparently this is solvable in one line by passing a specific argument.
I was thinking of just using SUC
over and over until we hit the right number but there is no way for me to compare the 2 Church numerals. I am completely stuck on this.
suppose you want to construct the function that give you the pred of N.
You have to use pairs on numbers
like this (0,1) (1,2) ....(n,n+1)
and construct the succPair
function
to go from (n-1,n) to (n,n+1)
and then you apply succPair
N times on (0,1)
and last step you just apply snd
on the last result I described
and then boom! you get your pred of N
look here to get the picture http://m2-info-upmc.blogspot.fr/2012/11/predecesseur-sur-les-entiers-de-church.html
You must have forced it to by an int in your subPred.
精彩评论