Haskell 转换或处理多种自定义数据类型

问题描述

我是 Haskell 的新手,正在尝试过滤特定的自定义数据类型。我一直在阅读 Graham Hutton 的 Programming in 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