Kevin Meredith
Kevin Meredith

Reputation: 41939

More Concise Pattern Matching on ADT with Multiple Data Types?

Let's say I have the following Algebraic Data Type:

data Foo = Bar Int | Baz Int | Bippy

I'm writing a function that, given a Foo, returns True for anything but Bippy.

Is there a more concise way to implement this function than pattern matching on all data types?

f :: Foo -> Bool
f (Bar _) = True
f (Baz _) = True
f Bippy = False

In this example, it only took 3 lines of code. But, what if I had 10 data types?

Upvotes: 1

Views: 103

Answers (1)

rampion
rampion

Reputation: 89123

How about

f :: Foo -> Bool
f Bippy = False
f _ = True

And you don't have 3 data types, you've got 3 constructors for 1 data type.

Upvotes: 6

Related Questions