Skip to content

Instantly share code, notes, and snippets.

@yonta
Last active June 20, 2019 07:33
Show Gist options
  • Save yonta/903ae7fbc4ec03e4091dd256c1145b6b to your computer and use it in GitHub Desktop.
Save yonta/903ae7fbc4ec03e4091dd256c1145b6b to your computer and use it in GitHub Desktop.
Added FizzBuzz
(*
* A original problem is here.
* https://twitter.com/kokuyouwind/status/1141602930019061760
*)
fun fizzBuzz n =
let
val div3 = n mod 3 = 0
val div5 = n mod 5 = 0
in
case (div3, div5) of
(true, true) => "fizzbuzz"
| (true, false) => "fizz"
| (false, true) => "buzz"
| (false, false) =>
Int.toString n
end
(*
* If a arg2 of function is true, the divisible is reversed.
*
* arg2 | divN | case
* ---------------------
* true | true | false
* true | false | true
* false | true | true
* false | false | false
*
* equals xor!!
*)
fun xor (b1, b2) = not (b1 = b2)
fun fizzBuzzPlus1 n b =
let
val div3 = n mod 3 = 0
val div5 = n mod 5 = 0
in
case (xor (div3, b), xor (div5, b)) of
(true, true) => "fizzbuzz"
| (true, false) => "fizz"
| (false, true) => "buzz"
| (false, false) =>
Int.toString n
end
fun fizzBuzzPlus2 n b =
let
fun xor3 (b1, b2, b3) = xor (xor (b1, b2), b3)
val div3 = n mod 3 = 0
val div5 = n mod 5 = 0
val neg = n < 0
in
case (xor3 (div3, b, neg), xor3 (div5, b, neg)) of
(true, true) => "fizzbuzz"
| (true, false) => "fizz"
| (false, true) => "buzz"
| (false, false) =>
Int.toString n
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment