2015-11-04 152 views
0

我想了解OCaml中的Y組合器。我從here獲取了一些代碼,我正在嘗試使用它來編寫Ackermann函數。在鏈接的例子中,這些函數只需要一個參數。 Ackermann函數需要兩個參數,並且因爲它而一直存在語法錯誤。我到目前爲止的代碼是如何使用ocaml中的Y組合函數調用具有多個參數的函數?

type 'a mu = Roll of ('a mu -> 'a);; 

let unroll (Roll x) = x;; 

let fix f = (fun x a -> f (unroll x x) a) (Roll (fun x a -> f (unroll x x) a));; 

let acker f = function 
    0, n -> n + 1 
| m, 0 -> f (m-1) 1 
| m, n -> f (m-1) (f m (n-1)) 
;; 

print_int (fix (acker 2 2));; 

我需要做些什麼才能使它起作用?謝謝。

回答

3

您正在混合curcry函數和uncurried函數定義。

這裏是阿克爾的一貫uncurried形式:

let acker f = function 
    0, n -> n + 1 
| m, 0 -> f (m - 1, 1) 
| m, n -> f (m - 1, f (m, n - 1));; 

下面是一個電話:

# fix acker (2, 2);; 
- : int = 7 
# 
相關問題