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
Example:
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?
Do I have to create e.g.:
isA :: SampleType - Bool
isA A = True
isA _ = False
for every constructor and use find?
It
On Tue, Jun 3, 2008 at 5:11 PM, Adam Smyczek [EMAIL PROTECTED] wrote:
Example:
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?
Do I have to create e.g.:
isA :: SampleType -
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?
Here's one way to do it:
filter (\x - case x of A - True; otherwise - False) sampleTypes
== [A]
filter
There's always one more way to do things in Haskell! :)
Here's yet another way to get at the payloads in a list. You don't have
to know how this works to use it:
data SampleType = A | B Int | C String
unA :: SampleType - [()]
unA A = return ()
unA _ = fail Not an A
unB :: SampleType
adam.smyczek:
Example:
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?
Do I have to create e.g.:
isA :: SampleType - Bool
isA A = True
isA _ = False
for every
On Tue, Jun 3, 2008 at 6:48 PM, Ronald Guida [EMAIL PROTECTED] wrote:
filter (\x - case x of A - True; otherwise - False) sampleTypes
== [A]
filter (\x - case x of B _ - True; otherwise - False) sampleTypes
== [B 5]
There's a neat little mini-trick for these types of pattern matches: