swing1234
swing1234

Reputation: 233

Haskell: Determine type of parameter in data type

Let's say I define a data type as follows:

data Type = MyInt |
            MyBool |
            MyFun Type Type 

and I have a variable type_a = MyFun MyInt MyBool

and I have another variable type_b = MyInt

How would I check if type_b = the first parameter in type_a (MyInt)?

Upvotes: 0

Views: 70

Answers (1)

Travis
Travis

Reputation: 435

You can derive Eq then use pattern matching to destructure MyFun and compare the first argument to 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

Upvotes: 2

Related Questions