efficiently checking that all the elements of a (big) list are the same

前端 未结 9 1065
清酒与你
清酒与你 2020-12-14 16:01

Problem

Let us suppose that we have a list xs (possibly a very big one), and we want to check that all its elements are the same.

I came up wi

9条回答
  •  执笔经年
    2020-12-14 16:47

    While not very efficient (it will traverse the whole list even if the first two elements don't match), here's a cheeky solution:

    import Data.List (group)
    
    allTheSame :: (Eq a) => [a] -> Bool
    allTheSame = (== 1) . length . group
    

    Just for fun.

提交回复
热议问题