Haskell:判断数据类型中参数的类型

Haskell: Determine type of parameter in data type

假设我定义了一个数据类型如下:

data Type = MyInt |
            MyBool |
            MyFun Type Type 

我有一个变量type_a = MyFun MyInt MyBool

我还有另一个变量type_b = MyInt

如何检查 type_b 是否为 type_a (MyInt) 中的第一个参数?

您可以导出 Eq 然后使用模式匹配来解构 MyFun 并将第一个参数与 type_b 进行比较:

data Type = MyInt | MyBool | MyFun Type Type deriving Eq

type_a = MyFun MyInt MyBool
type_b = MyInt

firstArgEquals :: Type -> Type -> Bool
firstArgEquals (MyFun a _) b = a == b
firstArgEquals _ _ = False

firstArgEquals type_a type_b -- returns True