没有区别:它们的语义完全相同。
tupleToList [] = []
tupleToList ((a,b):xs) = a : b : tupleToList xs
相当于
tupleToList ((a,b):xs) = a : b : tupleToList xs
tupleToList [] = []
这相当于
tupleToList ((a,b):xs) = a : b : tupleToList xs
tupleToList _ = []
通常,_当我们需要对多个案例进行模式匹配时,会使用通配符。例如
myAnd :: Bool -> Bool -> Bool
myAnd True y = y
myAnd False _ = False
上面,我们可以单独列举False False和False True情况,并使用三个方程,但使用通配符更方便(并且使我们的函数稍微懒惰,例如myAnd False undefined计算为False)。
最后,这主要是风格问题。通常,当通配符_只能代表一种情况时,最好把它拼出来,明确地说明它。这样,代码通常更具可读性。例如
not :: Bool -> Bool
not False = True
not _ = False
等效但比差
not :: Bool -> Bool
not False = True
not True = False
case当然,表达也是如此。
case x of
Just y -> 1 + y
Nothing -> 0
可以说比
case x of
Just y -> 1 + y
_ -> 0