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
fun fizz_buzz num = | |
let | |
val remainders = (num mod 3, num mod 5) | |
in | |
case remainders of | |
(0, 0) => "FizzBuzz" | |
| (0, _) => "Fizz" | |
| (_, 0) => "Buzz" | |
| _ => Int.toString(num) | |
end | |
fun fizz_buzz_up_to n = | |
map fizz_buzz (List.tabulate(n, fn x => x + 1)) | |
val test_not_multiple_of_three_nor_five = | |
fizz_buzz(1) = "1" | |
val test_other_not_multiple_of_three_nor_five = | |
fizz_buzz(2) = "2" | |
val test_three = fizz_buzz(3) = "Fizz" | |
val test_other_multiple_of_three = fizz_buzz(9) = "Fizz" | |
val test_five = fizz_buzz(5) = "Buzz" | |
val test_other_multiple_of_five = fizz_buzz(10) = "Buzz" | |
val test_multiple_of_three_and_five = fizz_buzz(15) = "FizzBuzz" | |
val test_first_fifteen_numbers = | |
fizz_buzz_up_to 15 = | |
["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"] |
You can check the code in this Bitbucket repository.
No comments:
Post a Comment