Move History

Fork Selected
  • Algorithms
    Logic
    Description

    What is the easiest way to check if all elements are equal in a Haskell list? Perhaps something that is safer than using head or tail?

    For instance:

    allEqual [1,2,3,4] = False 
    allEqual [1,1,1,1] = True 
    
    Code
    module AllEqual where
    
    allEqual :: [Int] -> Bool 
    allEqual xs = and $ map (== head xs) (tail xs)
    Test Cases
    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