Algorithms
Logic
Removed unsafe use of head
and tail
. However, nub
has a runtime complexity of O(n^2)
. That would make this function very inefficient for large lists...
module AllEqual where import Data.List allEqual :: [Int] -> Bool allEqual xs = length (nub xs) <= 1
- module AllEqual where
- import Data.List
- allEqual :: [Int] -> Bool
allEqual xs = and $ map (== head xs) (tail xs)- allEqual xs = length (nub xs) <= 1
import Test.Hspec import AllEqual main = hspec $ do 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
- import Test.Hspec
- import AllEqual
- main = hspec $ do
- 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