[英]Haskell Converting or Dealing with Multiple Custom Data Types
I am new to Haskell and am attempting to filter specific Custom Data types.我是 Haskell 的新手,我正在尝试过滤特定的自定义数据类型。 I have been going through the Programming in Haskell book by Graham Hutton but seem to just be confusing myself more and more.
我一直在阅读 Graham Hutton 的 Haskell 书中的编程,但似乎越来越让自己感到困惑。
So given the following type definitions:所以给出以下类型定义:
data Suit= Hearts
deriving Eq
data Rank= Numeric Int | Jack
deriving Eq
data Card= NormalCard Rank Suit | Joker
deriving Eq
I am trying to create a condition that filters out specific Cards, ie我正在尝试创建一个过滤掉特定卡片的条件,即
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
The issue is the following error问题是以下错误
Couldn't match expected type ‘Card’ with actual type ‘Rank’
I was wondering how to handle the conditions given that a 'Rank' should be considered a "Card"?我想知道如何处理“等级”应该被视为“卡”的条件?
You need to deconstruct i
in this case:在这种情况下,您需要解构
i
:
isAce = case i of
NormalCard r s -> r == Jack
_ -> False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.