Implementation of null function

后端 未结 3 896
盖世英雄少女心
盖世英雄少女心 2021-01-17 07:35

I have to learn Haskell for university and therefor I\'m using learnyouahaskell.com for the beginning.
I always used imperative languages so I decided to practice Haskell

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-17 07:58

    Because _ literally means anything apart from explicitly specified patterns. When you specify (_:_) it means anything which can be represented as a list containing at least 1 element, without bothering with what or even how many elements the list actually contains. Since the case with an explicit pattern for empty list is already present, (_:_) might as well be replaced by _.

    However, representing it as (_:_) gives you the flexibility to not even explicitly pass the empty list pattern. In fact, this will work:

    -- | Test whether a list is empty.
    null                    :: [a] -> Bool    
    null (_:_)              =  False
    null _                  =  True
    

    Demo

提交回复
热议问题