The Fizz Buzz Test in OCaml

I just found out about the Fizz Buzz Test yesterday, and I was so excited about solving it with the minimal OCaml knowledge I have!  So I did it!  You can find it on my github.

While writing this program, I learned that semicolons are helpful markers!  You are to separate each expressions with a single semicolon.  You are to separate each definition or function call with two semicolon.  E.g.,

let fizzbuzztest x = 
  if x mod 3 = 0 then 
    if x mod 5 = 0 then
      print_string ("fizzbuzz")let fizzbuzztest x = 
  if x mod 3 = 0 then 
    if x mod 5 = 0 then
      print_string ("fizzbuzz")
    else
        print_string ("fizz")
  else
    if x mod 5 = 0 then
      print_string ("buzz")
    else
      print_int x ; (*end of if statement*)
  print_newline () ;; (*end of definition of fizzbuzztest *)

Or

let rec counter_fizzbuzz n =
  match n with
    0 -> () 
    | _ -> counter_fizzbuzz (n - 1) ; (*end of first expression in match with _ *)
           fizzbuzztest n ;; (*end of definition of counter_fizzbuzz *)

counter_fizzbuzz 100 ;;

Apparently a large percentage of job candidates cannot pass the test.  I feel so encouraged that I can do this!


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.