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

前端 未结 9 1034
清酒与你
清酒与你 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:54

    Here is another version (don't need to traverse whole list in case something doesn't match):

    allTheSame [] = True
    allTheSame (x:xs) = isNothing $ find (x /= ) xs
    

    This may not be syntactically correct , but I hope you got the idea.

提交回复
热议问题