-
AlgorithmsLogic
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
Output:
-
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}