-3

I have the following loop:

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

And I have the following println function "I must use in this way";

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

In order to print the following:

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

When I run it, I get the following error on "printf "%s\n" (show x)" line:

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

Where is my mistake? How can I fix it?

I want to print the following value:

"B* K K K”
T-Heron
  • 5,385
  • 7
  • 26
  • 52
yusuf
  • 3,591
  • 8
  • 45
  • 86
  • 2
    No enough context to reason your error. Try making your problem self-contained but small enough so that ppl can read. – camlspotter Apr 12 '17 at 07:49
  • what else I am gonna write? I wrote whole the code. – yusuf Apr 12 '17 at 07:52
  • Okay smartass, just downvote me. Because it seems like you don't have better thing to do, just you don't understand something, and you downvote it. This is your character. – yusuf Apr 13 '17 at 11:40

1 Answers1

2

Don't pass show to println:

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

Also, "I" ^ acc should probably be "K" ^ acc for the K case.

Make sure that you are using ;; to separate terms at the toplevel. If you have

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

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

The println and (`App ...) will be considered arguments to the printf. Separate them like this:

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

println (`App(`App(`App(`Bstar, `K), `K), `K))
gsg
  • 9,167
  • 1
  • 21
  • 23