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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.