null instead of ==

后端 未结 3 1875
自闭症患者
自闭症患者 2021-02-05 01:31

I have just started to learn Haskell out of interest. I follow learnyouahaskell.com.

There I found this:

null checks if a list is emp

3条回答
  •  心在旅途
    2021-02-05 02:02

    In my opinion, null myList reads more naturally than myList == [].

    But the raison d'être for null is that it can be used as a function. For example, here's a function that takes a list of lists, and returns only the nonempty ones:

    nonemptyLists :: [[a]] -> [[a]]
    nonemptyLists = filter (not . null)
    

    Without null, this would be more awkward:

    nonEmptyLists = filter ([] /=)
    

提交回复
热议问题