Skip to content

Instantly share code, notes, and snippets.

@Codesleuth
Last active August 29, 2015 13:56
Show Gist options
  • Save Codesleuth/9132702 to your computer and use it in GitHub Desktop.
Save Codesleuth/9132702 to your computer and use it in GitHub Desktop.
module Main where
import Test.Hspec
someNumbers2 :: [Int]
someNumbers2 = 1:2:3:[]
-- someNumbers3 = 4:5:[1,2,3]
mapList2 :: (Int -> Int) -> [Int] -> [Int]
mapList2 f [] = []
mapList2 f (h:t) = (f h):(mapList2 f t)
type Bag = [(Char, Int)]
listToBag :: [Char] -> Bag
listToBag l = foldr (bagInsert) [] l
bagInsert :: Char-> Bag -> Bag
bagInsert c ((a,i) : b)
| c == a = (a, i + 1):b
| otherwise = (a,i):(bagInsert c b)
bagInsert c [] = (c, 1):[]
bagEqual :: Bag -> Bag -> Bool
bagEqual b sb
| b == sb = True
| otherwise = False
main :: IO ()
main = hspec $ do
describe "Bag" $ do
describe "Adding" $ do
it "Adds 'a' to empty Bag" $ do
bagInsert 'a' emptybag `shouldBe` bagofa
it "Adds 'b' to Bag of 'a'" $ do
bagInsert 'b' bagofa `shouldBe` bagofba
it "Adds 'a' to Bag of 'a'" $ do
bagInsert 'a' bagofa `shouldBe` bagof2a
describe "listTobag" $ do
it "adds a list to a bag" $ do
listToBag ['a','a','b'] `shouldBe` bagof2a1b
describe "bagEqual" $ do
it "true" $ do
bagEqual bagofa bagofa `shouldBe` True
it "false" $ do
bagEqual bagofa bagof2a `shouldBe` False
it "different order true" $ do
bagEqual bagofab bagofba `shouldBe` True
where
emptybag = []::Bag
bagofa = [('a',1)]::Bag
bagof2a = [('a',2)]::Bag
bagofab = [('b',1),('a',1)]::Bag
bagofba = [('a',1),('b',1)]::Bag
bagof2a1b = [('b',1),('a',2)]::Bag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment