2014-09-11 60 views
0

我有以下數據類型:有沒有辦法在Haskell的列表中加一個Enum Bounded Ord類型?

data Suit = Club | Diamond | Heart | Spade 
     deriving (Eq, Ord, Bounded, Enum) 

data Rank = 
R2 | R3 | R4 | R5 | R6 | R7 | R8 | R9 | R10 | 
Jack | Queen | King | Ace 
    deriving (Eq, Ord, Bounded, Enum) 

data Card = Card {suit::Suit, rank::Rank} 
     deriving (Eq, Bounded) 

,現在有[Card]列表。有沒有辦法讓我在列表中加一個CardRank?例如給出一個清單[Card Club R2, Card Club R2]我想使它[Card Club R3, Card Club R3]。我寫了下面的代碼:

-- | Update certain number of cards' rank in the list. 
updateRankList :: Int -> [Card] -> [Card] 

updateRankList num (x : xs) 
    | num == 0 = [] 
    | otherwise = updateRank x ++ updateRankList (num - 1) xs 

-- | Update a card's rank, didn't finish. 
updateRank :: Card -> Card 

updateRank card = 

而我卡住了。誰能幫我?謝謝。

+3

一如既往,請想想邊界條件,並更新你的問題。 – 2014-09-11 13:12:32

+0

對不起,我不太清楚您的邊界條件是什麼意思。 – user3928256 2014-09-11 13:21:48

+3

「Ace」的'plus'級別是什麼?它應該環繞到「R2」嗎? – Lee 2014-09-11 13:22:48

回答

0

你可以做一個包裝SUCC爲界與枚舉類型:

succWrap x = if x == maxBound then minBound else succ x 

,那麼你只需要

updateRanks n xs = map succWrap (take num xs) ++ drop num xs 
0
:info Enum 

非常有用!我正在尋找的功能是succ,謝謝你們。

相關問題