Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Haskell Sum Type Pattern Matching: how to specify some cases but not other cases?

I have the following data type:

data TestType a = TypeA a
     | TypeB a
     | TypeC a

How can I simplify the pattern matching of the following function:

f (TypeA x) (TypeB y) = "No"
f (TypeA x) (TypeC y) = "No"
f (TypeB x) (TypeA y) = "No"
f (TypeB y) (TypeC x) = "No"
f (TypeC y) (TypeA x) = "No"
f (TypeC y) (TypeB x) = "No"
f (TypeA x) (TypeA y) = "yes!"

In summary, I should only be returning "yes!" if I am receiving two TypeA data inputs, otherwise, return "No".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Implement the case for two TypeAs and use wildcards for the other cases:

f :: TestType a -> TestType b -> String
f (TypeA _) (TypeA _) = "yes!"
f _ _ = "No"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading