Skip to content

Instantly share code, notes, and snippets.

@sroccaserra
Last active December 8, 2017 08:14
Show Gist options
  • Select an option

  • Save sroccaserra/bbe04169bf49a79e5cb60cbc319a857e to your computer and use it in GitHub Desktop.

Select an option

Save sroccaserra/bbe04169bf49a79e5cb60cbc319a857e to your computer and use it in GitHub Desktop.
Dojo
import Test.Hspec
-- ## Prerequisite
--
-- $ brew install ghc
-- $ cabal update
-- $ cabal install hspec
-- ## Running
--
-- $ runhaskell Test.hs
-- ## Mini spec
--
-- 1: "1"
-- 2: "2"
-- 3: "fizz"
-- 4: "4"
-- 5: "buzz"
-- ...
--15: "fizzbuzz"
fizzbuzz :: Integer -> String
fizzbuzz x
| (0 == x `mod` 3) && (0 == x `mod` 5) = "fizzbuzz"
| 0 == x `mod` 3 = "fizz"
| 0 == x `mod` 5 = "buzz"
| otherwise = show x
add :: Integer -> Integer -> Integer
add x y = x + y
add4 :: Integer -> Integer
add4 = add 4
main = hspec $ do
describe "Examples" $ do
it "Backtick notation allows infix use of prefix functions" $ do
shouldBe (mod 3 3) 0
mod 3 3 `shouldBe` 0
3 `mod` 3 `shouldBe` 0
it "In Haskell, all functions really are curried, one-argument functions (!!)" $ do
(add 4) 5 `shouldBe` 9
add4 5 `shouldBe` 9
let add3 = add 3
add3 1 `shouldBe` 4
describe "Regular numbers" $ do
it "1 return '1'" $ do
fizzbuzz 1 `shouldBe` "1"
it "2 return '2'" $ do
fizzbuzz 2 `shouldBe` "2"
it "3 return 'fizz'" $ do
fizzbuzz 3 `shouldBe` "fizz"
it "5 return 'buzz'" $ do
fizzbuzz 5 `shouldBe` "buzz"
it "6 return 'fizz'" $ do
fizzbuzz 6 `shouldBe` "fizz"
it "15 return 'fizzbuzz'" $ do
fizzbuzz 15 `shouldBe` "fizzbuzz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment