ocaml 表达式类型不匹配

ocaml expression type 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 函数 "I must use in this way";

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”

不要将 show 传递给 println

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

此外,"I" ^ acc 对于 K 情况可能应该是 "K" ^ acc

确保您使用 ;; 在顶层分隔术语。如果你有

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))