Ad
Algorithms
Logic

All equal is a property of a whole list. One of possile ways is to use folds that are more natual way to cacluate/check value/property on a foldable structures.

Also algorithm has O(n) complexity

Code
Diff
  • module AllEqual where
    
    import Data.Foldable(foldl')
    
    allEqual :: [Int] -> Bool
    allEqual []     = True
    allEqual (x:xs) = foldl' (\b a -> b && (a==x)) True xs
    • module AllEqual where
    • import Data.List
    • allEqual :: [Int] -> Bool
    • allEqual xs = length (nub xs) <= 1
    • import Data.Foldable(foldl')
    • allEqual :: [Int] -> Bool
    • allEqual [] = True
    • allEqual (x:xs) = foldl' (\b a -> b && (a==x)) True xs