Created
September 19, 2015 16:46
-
-
Save esehara/2c02b447d14d6a55fe06 to your computer and use it in GitHub Desktop.
OCaml完全マスターした (OCamlでFizzBuzzを書いてみよう) ref: http://qiita.com/esehara@github/items/bfb05d84ee1f8357276a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let hello = print_string ("Hello, World." ^ "\n") | |
let () = hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print_string "Hello, World." ^ "\n" | |
(* => (print_string "Hello, World.") ^ "\n" *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fizz x = | |
if x mod 3 = 0 then "Fizz" | |
else "" | |
let buzz x = | |
if x mod 5 = 0 then "Buzz" | |
else "" | |
let fizzbuzz x : unit = | |
let rec fizzbuzz_iter x y = | |
let str_fizzbuzz = (fizz x) ^ (buzz x) in | |
(if str_fizzbuzz = "" then print_int x | |
else print_string str_fizzbuzz); | |
print_string "\n"; | |
if x = y then () | |
else fizzbuzz_iter (x + 1) y in | |
fizzbuzz_iter 1 x | |
let () = | |
fizzbuzz 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec range a b = | |
if a > b then [] | |
else a :: range (a+1) b | |
let fizzbuzz x = | |
match (x mod 15) with | |
0 -> "FizzBuzz" | |
| 3 -> "Fizz" | |
| 5 -> "Buzz" | |
| 6 -> "Fizz" | |
| 9 -> "Fizz" | |
| 10 -> "Buzz" | |
| _ -> string_of_int x | |
let () = | |
List.iter (fun x -> (Printf.printf "%s\n" (fizzbuzz x))) (range 1 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment