Algorithms
Logic
Using all
to communicate semantics
module AllEqual where allEqual :: [Int] -> Bool allEqual [] = True allEqual (x:xs) = all (== x) xs
- module AllEqual where
import Data.Foldable(foldl')- allEqual :: [Int] -> Bool
- allEqual [] = True
allEqual (x:xs) = foldl' (\b a -> b && (a==x)) True xs- allEqual (x:xs) = all (== x) xs
module AllEqualSpec where import Test.Hspec import AllEqual spec = describe "All Equal" $ do it "Basic tests" $ do allEqual [1,2,3,4] `shouldBe` False allEqual [1,1,1,1] `shouldBe` True allEqual [] `shouldBe` True allEqual [1] `shouldBe` True
- module AllEqualSpec where
- import Test.Hspec
- import AllEqual
main = hspec $ do- spec =
- describe "All Equal" $ do
- it "Basic tests" $ do
- allEqual [1,2,3,4] `shouldBe` False
- allEqual [1,1,1,1] `shouldBe` True
- allEqual [] `shouldBe` True
- allEqual [1] `shouldBe` True