2017-04-12 65 views
-3

我有以下循環:OCaml的表達式類型unmatchings

let show expr = 
    let rec loop acc = function 
    | `S -> "S"^acc 
    | `K -> "I"^acc 
    | `I -> "I"^acc 
    | `App(a,b) -> (loop acc a)^(loop acc b) 
    | `B -> "B"^acc 
    | `C -> "C"^acc 
    | `Sprim -> "S'"^acc 
    | `Bprim -> "B'"^acc 
    | `Bstar -> "B*"^acc 
    | `Cprim -> "C'"^acc 
    | `Var(a) -> a^acc 
    | `Con(a) -> a^acc 
    in 
    loop "" expr 

和我有以下println函數「我必須以這種方式使用」;

let println x = 
     printf "%s\n" (show x) 

爲了打印以下:

println (`App(`App(`App(`Bstar, `K), `K), `K));; 

當我運行它,我得到"printf "%s\n" (show x)"線以下錯誤:

Error: This expression has type 
     ('a -> 'b -> 'c, out_channel, unit, unit, unit, 'a -> 'b -> 'c) 
     CamlinternalFormatBasics.fmt 
     but an expression was expected of type 
     ('a -> 'b -> 'c, out_channel, unit, unit, unit, unit) 
     CamlinternalFormatBasics.fmt 
     Type 'a -> 'b -> 'c is not compatible with type unit 

哪裏是我的錯?我該如何解決它?

我想打印以下值:

"B* K K K」 
+2

沒有足夠的上下文來推理您的錯誤。嘗試讓自己的問題獨立,但足夠小,以便體力勞動可以閱讀。 – camlspotter

+0

我還會寫什麼?我寫了整個代碼。 – yusuf

+0

好的smartass,只是downvote我。因爲看起來你沒有更好的事情去做,只是你不明白某些事情,而且你不喜歡它。這是你的角色。 – yusuf

回答

2

千萬不要錯過showprintln

println (`App(`App(`App(`Bstar, `K), `K), `K)) 

此外,"I"^acc也許應該"K"^acc用於K情況。

確保您使用;;來分隔頂層的術語。如果你有

let println x = 
    Printf.printf "%s\n" (show x) 

println (`App(`App(`App(`Bstar, `K), `K), `K)) 

println(`App ...)將被視爲參數的printf。像這樣分開它們:

let println x = 
    Printf.printf "%s\n" (show x) 
;; 

println (`App(`App(`App(`Bstar, `K), `K), `K)) 
+0

我仍然收到錯誤消息。我正在編輯這個問題,並會在那裏輸入錯誤。 – yusuf