我是函数式编程和F#的初学者。作为练习,我试图实现教堂数字。
首先,我将数字编码为:
let ZERO = fun p x -> x
let ONE = fun p x -> p x
let TWO = fun p x -> p(p x)
let THREE = fun p x -> p(p(p x))
let FIVE = fun p x ->p(p(p(p(p x))))
... etc
然后我编写了辅助函数,以查看我的数字是否有效:
let toInt p = p (fun x->x+1) 0
例如:
toInt (THREE) |> printfn "%A"
按预期打印3张。
我还实现了一些自动功能:
let INC = fun n f x -> f((n(f))(x)) //incrementation
let DEC = fun n f x -> n (fun g -> fun h -> h(g f))(fun y->x)(fun y-> y) //decrementation
let ADD = fun m n -> n(INC)(m) //addition
let MUL = fun m n s z -> n(m s) z //multiplication
let POW = fun m n -> n(MUL m)ONE //exponential
它们似乎都可以正常工作
toInt (INC THREE) |> printfn "%A" //prints 4
toInt (DEC THREE) |> printfn "%A" //prints 2
toInt (ADD THREE FIVE) |> printfn "%A" //prints 8
toInt (MUL THREE FIVE) |> printfn "%A" //prints 15
toInt (POW THREE FIVE) |> printfn "%A" //prints 243
但是我真的很难实现减法:
let SUB = fun m n -> n(DEC m)
toInt (SUB FIVE THREE) |> printfn "%A" //gives 64
上述方法看起来像某种指数形式,而不是相减。
我也尝试过(如Wiki所示):
let SUB = fun m n -> n(DEC)m
但是当我尝试使用它时会导致类型不匹配:
toInt (SUB FIVE THREE) |> printfn "%A"
"Type mismatch. Expecting a
'(((('a -> 'b) -> ('b -> 'c) -> 'c) -> ('d -> 'e) -> ('f -> 'f) -> 'g) -> 'a -> 'e -> 'g) -> (('h -> 'h) -> 'h -> 'h) -> (int -> int) -> int -> 'i'
but given a
'(((('a -> 'b) -> ('b -> 'c) -> 'c) -> ('d -> 'e) -> ('f -> 'f) -> 'g) -> (('a -> 'b) -> ('b -> 'c) -> 'c) -> ('d -> 'e) -> ('f -> 'f) -> 'g) -> ((('a -> 'b) -> ('b -> 'c) -> 'c) -> ('d -> 'e) -> ('f -> 'f) -> 'g) -> (('a -> 'b) -> ('b -> 'c) -> 'c) -> ('d -> 'e) -> ('f -> 'f) -> 'g'
The types ''a' and '('a -> 'b) -> ('b -> 'c) -> 'c' cannot be unified"
我被困住了,我在做什么错? 而且,我也很感谢任何有关改进我的代码的建议。