McCarthy 91 function by hebiyan

let rec show n = function
    0 -> Printf.sprintf "%d" n
  | l -> Printf.sprintf "M(%s)" (show n (l - 1)) ;;

let rec mc91 n =
  let rec inner n level =
    if (100 < n) then (
      let rv = n - 10 in
      Printf.printf "%s\n" (show rv (pred level));
      rv
    )
    else (
      let next = n + 11 in
      Printf.printf "%s\n" (show next (level + 1));
      let nv = inner next (level + 1) in
      inner nv level
    )
  in
  Printf.printf "%s\n" (show n 1);
  inner n 1 ;;
  
mc91 (read_int ());;

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page