Move History

Rooted by: TDD Game: sum
Fork Selected
  • Code
    module Sum (Sum.sum) where
    
    import Prelude hiding (sum)
    
    sum :: (Foldable t) => t Integer -> Integer
    sum = foldr (+) 0
    Test Cases
    module SumSpec (spec) where
    
    import Prelude hiding (sum)
    import qualified Prelude as Pre (sum)
    import Sum (sum)
    import Test.Hspec
    
    spec :: Spec
    spec = do
      it "tests" $ do
        Sum.sum [1] `shouldBe` Pre.sum [1]
      it "Empty list" $ do
        Sum.sum [] `shouldBe` Pre.sum []
      it "Different container" $ do
        Sum.sum (Just 1) `shouldBe` Pre.sum (Just 1)
      it "negative numbers" $ do
        Sum.sum [-1] `shouldBe` Pre.sum [-1]
      it "big numbers" $ do
        Sum.sum [18446744073709551615] `shouldBe` Pre.sum [18446744073709551615]
  • Code
    • module Sum (Sum.sum) where
    • import Prelude hiding (sum)
    • sum :: (Foldable t) => t Integer -> Integer
    • sum xs | null xs = 0 | otherwise = foldr1 (+) xs
    • sum = foldr (+) 0