user24353
user24353

Reputation: 37

variable constructor selection

I am writing a haskell program and I'm new to haskell. I have a user defined data type and trying to use it in a function as an argument. In my function implementation I need to distinguish different constructors used to produce the data. What can I do?

data myData = C1 Int | C2 String

myFunc :: myData -> Int
myFunc c from constructor C1 = 0
myFunc c from constructor C2 = 1

Upvotes: 0

Views: 65

Answers (1)

chtenb
chtenb

Reputation: 16184

Like this? (I suppose you want to pattern match on the constructors)

myFunc :: myData -> Int
myFunc (C1 _) = 0
myFunc (C2 _) = 1

Upvotes: 3

Related Questions