Skip to content

Instantly share code, notes, and snippets.

@mattyw
Created September 13, 2011 19:49
Show Gist options
  • Save mattyw/1214902 to your computer and use it in GitHub Desktop.
Save mattyw/1214902 to your computer and use it in GitHub Desktop.
Fizzbuzz in Haskell using guards
import Test.HUnit
fizzbuzz :: Int -> String
fizzbuzz x
| x `mod` 15 == 0 = "fizzbuzz"
| x `mod` 3 == 0 = "fizz"
| x `mod` 5 == 0 = "buzz"
| True = show x
test1 = TestCase (do assertEqual "" "1" (fizzbuzz 1))
test2 = TestCase (do assertEqual "" "fizz" (fizzbuzz 3))
test3 = TestCase (do assertEqual "" "4" (fizzbuzz 4))
test4 = TestCase (do assertEqual "" "buzz" (fizzbuzz 5))
test5 = TestCase (do assertEqual "" "fizz" (fizzbuzz 6))
test6 = TestCase (do assertEqual "" "buzz" (fizzbuzz 10))
test7 = TestCase (do assertEqual "" "fizzbuzz" (fizzbuzz 15))
tests :: Test
tests = TestList [TestLabel "test1" test1,
TestLabel "test2" test2,
TestLabel "test3" test3,
TestLabel "test4" test4,
TestLabel "test5" test5,
TestLabel "test6" test6,
TestLabel "test7" test7]
main :: IO Counts
main = do runTestTT tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment