我正在尝试编写一个函数,该函数接受Cards列表,并给我所有等级值。我在功能上遇到了非穷尽模式的问题,我无法解决
data Card = Card Suit Rank
deriving (Show, Bounded, Read)
data Suit = Red
| Black
deriving (Show, Enum, Bounded, Read)
data Rank = Ace
| Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
deriving (Show, Enum, Bounded, Read)
handValue :: [Card] -> [Int]
handValue [Card s r]
| (length [Card s r]) < 0 = (fromEnum (r) + 1) : handValue (tail [Card s r])
| otherwise = []
我该如何解决这个问题?
答案 0 :(得分:2)
在:
handValue :: [Card] -> [Int]
handValue [Card s r]
您要为长度为1的列表作为第一个参数传递的情况定义函数。您还没有为其他情况定义函数-长度小于1或长度大于1。
[Card s r]
是一种匹配长度1列表的模式,并将其中的唯一值解包到s
和r
中。看来您想做更多类似的事情:
handValue cards
| (length cards) < 0 = ...
| otherwise = []
但这并不是很有意义,因为列表的长度永远不能小于0。
也许您的意思更像是:
handValue [] = []
handValue (Card s r):cards = (fromEnum r + 1) : handValue cards
与空列表和非空列表相匹配的模式,应该详尽无遗。