OCAML 表达式类型不匹配



我有以下循环:

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 "%sn" (show x)

为了打印以下内容:

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

当我运行它时,我在"printf "%sn" (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))

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

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

let println x =
  Printf.printf "%sn" (show x)
println (`App(`App(`App(`Bstar, `K), `K), `K))

println(`App ...)将被视为printf的论据。像这样将它们分开:

let println x =
  Printf.printf "%sn" (show x)
;;
println (`App(`App(`App(`Bstar, `K), `K), `K))

最新更新