我试图理解教会数字的取幂函数:
fun power m n f = n m f;
在其中,我看到了一个乘法。我知道这是错的,因为乘法是:
fun times m n f = m ( n f );
我认为已经明白了。
问题在于,我无法理解哪个函数会将教会号码的应用产生给另一个。
例如,这个表达式会产生什么?
( fn x => fn y => x ( x y ) ) ( fn x => fn y => x ( x ( x y ) ) );
由于
答案 0 :(得分:1)
如果你的计算结果是一个教会号码,你可以通过传递一个后继函数和零来计算它的int值:
(fn x=> x+1) 0
在你的例子中:
( fn x => fn y => x ( x y ) ) ( fn x => fn y => x ( x ( x y ) ) ) (fn x=> x+1) 0;
结果是:
val it = 9 : int
所以你计算了3 ^ 2
术语
( fn x => fn y => x ( x y ) ) ( fn x => fn y => x ( x ( x y ) ) )
缩减为
( fn x => fn y => x ( x ( x ( x ( x ( x ( x ( x ( x y ) ) ) ) ) ) ) ) )
但是sml不能减少到这个术语,它需要参数,所以它可以计算出一个具体的值。
使用Lambda Calculus的更好的语言是Haskell,因为它使用了懒惰的评估。
您可以减少术语
( fn x => fn y => x ( x y ) ) ( fn x => fn y => x ( x ( x y ) ) )
自己:
fn x => fn y => x (x y) (fn x => fn y => x (x (x y) ) )
reduce x with (fn x => fn y => x (x (x y) ) ):
fn y => (fn x => fn y => x (x (x y) ) ) ( (fn x => fn y => x (x (x y) ) ) y)
rename y to a in the last (fn x => fn y => x (x (x y) ) )
and rename y to b in the first (fn x => fn y => x (x (x y) ) ):
fn y => (fn x => fn b => x (x (x b) ) ) ( (fn x => fn a => x (x (x a) ) ) y)
reduce x in (fn x => fn a => x (x (x a) ) ) with y:
fn y => (fn x => fn b => x (x (x b) ) ) ( fn a => y ( y (y a) ) )
reduce x in (fn x => fn b => x (x (x b) ) ) with ( fn a => y ( y (y a) ) ):
fn y => fn b => ( fn a => y ( y (y a) ) ) ( ( fn a => y ( y (y a) ) ) ( ( fn a => y ( y (y a) ) ) b) )
we reduce a with b in the last term:
fn y => fn b => ( fn a => y ( y (y a) ) ) ( ( fn a => y ( y (y a) ) ) ( y ( y (y b) ) ) )
we reduce a with ( y ( y (y b) ) ) in the last term:
fn y => fn b => ( fn a => y ( y (y a) ) ) ( y ( y (y ( y ( y (y b) ) ) ) ) )
we reduce a with ( y ( y (y ( y ( y (y b) ) ) ) ) ) in the last term:
fn y => fn b => y ( y (y ( y ( y (y ( y ( y (y b) ) ) ) ) ) ) )
we are done!