[英]Haskell Converting or Dealing with Multiple Custom Data Types
我是 Haskell 的新手,我正在嘗試過濾特定的自定義數據類型。 我一直在閱讀 Graham Hutton 的 Haskell 書中的編程,但似乎越來越讓自己感到困惑。
所以給出以下類型定義:
data Suit= Hearts
deriving Eq
data Rank= Numeric Int | Jack
deriving Eq
data Card= NormalCard Rank Suit | Joker
deriving Eq
我正在嘗試創建一個過濾掉特定卡片的條件,即
filterCondition:: Card -> Bool
filterCondition i = isJack && isJoker
where
isAce = i == Jack -- error here: Couldn't match expected type ‘Card’ with actual type ‘Rank’
isJoker = i == Joker
問題是以下錯誤
Couldn't match expected type ‘Card’ with actual type ‘Rank’
我想知道如何處理“等級”應該被視為“卡”的條件?
在這種情況下,您需要解構i
:
isAce = case i of
NormalCard r s -> r == Jack
_ -> False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.