Hi, I'm Ataias Reis

Programmer, made in Brazil, Alma mater University of Brasilia

Hello World printing in OCaml

Another simple problem from HackerRank is to print Hello World multiple times in a functional language. Well, I am using OCaml and I suppose I should avoid loops. The way I solved it is the following:

open Core.Std
let n = read_int ();;

let rec print_hello n =
  if n > 0 then begin
      printf "Hello World\n";
      print_hello (n-1);
    end else ();;

print_hello n

Problems I had: I lost some some time to discover the ;; was required to make it compile properly. I am not sure exactly why yet. For instance, if I use else (); (just one semicolon) it doesn’t print anything, even though it compiles. Usually the semicolons are required on the toplevel, but on scripts it shouldn’t be always needed. If you know something about this, let me know in the comments.

Thanks for reading!

comments powered by Disqus