Last active
June 21, 2019 18:05
-
-
Save sean-clayton/3f240e7f70c3e0eef969d0f966c79607 to your computer and use it in GitHub Desktop.
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 fizzBuzz attempt = | |
match attempt with | |
| (0, 0, _) -> "FizzBuzz" | |
| (0, _, _) -> "Fizz" | |
| (_, 0, _) -> "Buzz" | |
| (_, _, n) -> n.ToString() | |
for num in 1..100 do | |
printfn "%s" (fizzBuzz (num % 3, num % 5, num)) | |
(* | |
fizzBuzz takes a single attempt. | |
An attempt is a 3-element tuple from a number from 1 to 100. | |
The 3-tuple structure looks like this: | |
1. The remainder of the number and 3 (to represent if it's divisible by 3) | |
2. The remainder of the number and 5 (to represent if it's dibisible by 5) | |
3. The number itself | |
So, with the first number in 1 through 100, we have 1, | |
which would have an attempt that looks like this: (1, 1, 1) | |
The remainder of 1 and 3 is 1, the remainder of 1 and 5 is 1, and the number itself is 1 | |
Skipping ahead to 3 would look like this: (0, 3, 3) | |
The remainder of 3 and 3 is 0, the remainder of 3 and 5 is 3, and the number itself is 3 | |
For 5: (2, 0, 5) | |
The remainder of 5 and 3 is 2, the remainder of 5 and 5 is 0, and the number itself is 5 | |
Skipping ahead to 15: (0, 0, 15) | |
The remainder of 15 and 3 is 0, the remainder of 15 and 5 is 0, and the number itself is 15 | |
The fizzBuzz function takes this 3-tuple and matches it against 4 cases: | |
(0, 0, _) -> "FizzBuzz" | |
(0, _, _) -> "Fizz" | |
(_, 0, _) -> "Buzz" | |
(_, _, n) -> n | |
The case of (0, 0, _) says "this number is divisible by both 3 and 5" and therefore we show "FizzBuzz" | |
(0, _, _) says "this number is not divisible by both 3 and 5, but is divisble by 3", so we show "Fizz" | |
(_, 0, _) says the same, but for its divisibility by 5 | |
(_, _, n) says that it's not divisible by 3 or 5, so we should just show the number itself | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment