Skip to content

Instantly share code, notes, and snippets.

@Nnwww
Created September 27, 2015 16:05
Show Gist options
  • Save Nnwww/4c4dc58683b46d563191 to your computer and use it in GitHub Desktop.
Save Nnwww/4c4dc58683b46d563191 to your computer and use it in GitHub Desktop.
FizzBuzz in OCaml
open List;;
let fizzbuzz min max =
let say cur =
match (cur mod 3, cur mod 5) with
| (0, 0) -> "FizzBuzz"
| (0, _) -> "Fizz"
| (_, 0) -> "Buzz"
| (_, _) -> string_of_int cur
in
let rec loop strs cur =
if cur < min then
strs
else
loop (say cur :: strs) (cur - 1)
in
let res = loop [] max in
map res (Printf.sprintf "%s");;
@Nnwww
Copy link
Author

Nnwww commented Sep 27, 2015

let fizzbuzz i = match i mod 3, i mod 5 with
    0, 0 -> "FizzBuzz"
  | 0, _ -> "Fizz"
  | _, 0 -> "Buzz"
  | _    -> string_of_int i

let () =
  for i = 1 to 100 do print_endline @@ fizzbuzz i done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment