[Haskell-cafe] A simple beginner question

Yitzchak Gale gale at sefer.org
Wed Jun 4 04:24:22 EDT 2008

Adam Smyczek wrote:
> data SampleType = A | B Int | C String | D -- .... etc.
> sampleTypes = [A, B 5, C "test"] :: [SampleType]
> How do I find for example element A in the sampleTypes list?

There have been many useful replies. But since Adam
originally announced that this is a "beginner question", I think
some perspective is in order.

In Haskell, there is often no need at all for boolean-valued
functions to deconstruct a data structure. Usually pattern
matching does the job beautifully, and you structure your program
to exploit that. In the case that you do need them, though, the
previous responses are excellent suggestions.

In my experience, the ways that I get data out of an ADT,
from most common to most rare, are:

1. Just use pattern matching
2. Use record syntax to get selector functions
3. Define an Eq instance
4. Define per-constructor modifiers when the ADT is used as
  state in a state monad (this one is admittedly a pain in the neck)

I can't remember the last time I needed to write a function
like "isA" - it almost never comes up.

My opinion, YMMV.


More information about the Haskell-Cafe mailing list