Seonil Baek
Seonil Baek

Reputation: 23

haskell pattern maching - many conditions matching with one expression

I wanna match many conditions with one expression in Haskell pattern matching.

for example,

data Message = HELLO | HI | GOODBYE | BYE

greeting x = case x of
  HELLO or HI          -> "hello"
  GOODBYE or BYE   -> "bye"

But I can't find how to do this.

sorry for my poor english. thank you.

Upvotes: 2

Views: 1852

Answers (1)

jtobin
jtobin

Reputation: 3273

Try guards. Ex,

data Message = HELLO | HI | GOODBYE | BYE deriving (Eq)

greeting x
    | x == HELLO   || x == HI   = "hello"
    | x == GOODBYE || x == BYE  = "bye"

Note you'll have to derive an Eq instance for your data type. Check out the relevant section of Learn you a Haskell.

Upvotes: 5

Related Questions