Searching through list

后端 未结 4 846
日久生厌
日久生厌 2021-01-26 14:47

I\'ve been trying to define a function which, given a list of Integers and an Integer n, returns a Boolean indicating whether n occurs exactly once in the list.

I have t

4条回答
  •  面向向阳花
    2021-01-26 15:07

    As I thought my former solution was ugly, I asked in another forum and got this as answer:

    once :: Eq a => a -> [a] -> Bool
    once x = (== [x]) . filter (== x)
    

    I think you can't write that function much nicer, and in contrast to the accepted answer it's lazy.

提交回复
热议问题