OCaml code submissions


#1

Hello,

I am new to coding in OCaml within CodingGame. I am trying the simple Chuck Norris puzzle, and I have a working code - as in, i have tested my code locally with utop, and it works. However I cannot figure out how am i supposed to enter it in the editor.

This is the code i’m currently entering :

type subseries = {
    bin_val : int;
    repeat : int;
    }
let int2series n =
  let rec bool_decomp n =
    if (n < 0) then raise (Failure "n < 0")
    else if (n == 0) then [0]
    else if (n == 1) then [1]
    else (n mod 2) :: (bool_decomp (n/2))
  in List.rev (bool_decomp n)

let series2subseries s : subseries list = 
  let ret = ref []
  and subserie = ref None
  in let series_iter e =
      match !subserie with
      | None -> (subserie := (Some { bin_val = e; repeat = 1 });)
      | Some { bin_val=v; repeat=rpt } ->
          if (v == e) then
            (subserie := Some { bin_val = e; repeat = rpt + 1 };)
          else
            (ret := { bin_val=v; repeat=rpt} :: !ret; subserie := Some { bin_val = e; repeat = 1 };)
    in (List.iter series_iter s; List.rev (match !subserie with None -> !ret
                                                              | Some ss -> ss :: !ret))

let string2series s = 
  let ret = ref []
  in let string_iter c =
    (ret := !ret @ int2series (Char.code c))
  in (String.iter string_iter s; !ret)

let string2subseries s : subseries list = series2subseries (string2series s)

let rec int2unary i =
  if (i == 0) then ""
  else String.concat "" ["0"; int2unary (i-1)]

let bin2int b =
  match b with
  | 0 -> 2
  | 1 -> 1
  | _ -> raise Not_found

let subseries2encoding l =
  let rec sl2e l = match l with
    | [] -> []
    | ss :: l -> (s2e ss) :: (sl2e l)
  and s2e ss =
    String.concat " " [int2unary (bin2int ss.bin_val); int2unary ss.repeat]
  in String.concat " " (sl2e l)

let string2encoding s = subseries2encoding (string2subseries s)

let message = input_line stdin in
print_endline "answer";

And the error I got is :

File “/tmp/Answer.ml”, line 60, characters 31-33:

(the line 60 is the one with let message = input_line stdin…)

How can I read what this Answer.ml file looks like ? How can I format my code in such a way that it will be accepted by the CG framework ?

Thanks


#2

Hello,

Answer.ml is the very file you quote above.

And there is indeed an error on Line 60, since “in” is not valid here (in compile mode).
You should do something like this instead:

let _ =
    let message = input_line stdin in
    print_endline "answer"

Notice that the final ; is also not valid.