检查长度(x:xs)

时间:2015-11-08 22:28:04

标签: haskell

我有类似这样的功能

checkSomething (x:xs) counter
| (length of x:xs)         = ...
| ...                      = ...  

我想检查列表(x:xs)是否为空。我以某种方式没有设法使用(x:xs)表示法。

1 个答案:

答案 0 :(得分:9)

x:xs永远不会为空:它总是有元素x。匹配空列表的唯一模式是[](如果它具体类型为"",则拼写为[Char])。所以,相反,你可以写

checkSomething [] counter = {- the empty case -}
checkSomething (x:xs) counter = {- lists with at least one element -}