[Haskell-cafe] Searching for ADT patterns with elem and
find
Mitchell, Neil
neil.mitchell.2 at credit-suisse.com
Wed Nov 12 08:53:12 EST 2008
> I guess one drawback compared to Neil's suggested use of "any" (and
staying with a separate "isTypeB") is that your solution will iterate
over the entire list, regardless of an early hit.
Nope, it will stop on the first one - Haskell is lazy like that :-)
Thanks, Neil
________________________________
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Paul Keir
Sent: 12 November 2008 1:45 pm
To: Tom Nielsen
Cc: haskell-cafe at haskell.org
Subject: RE: [Haskell-cafe] Searching for ADT patterns with elem
and find
Thanks Tom,
That is indeed a very elegant solution; I too often forget about
the wonders of list comprehension.
I guess one drawback compared to Neil's suggested use of "any"
(and staying with a separate "isTypeB") is that your solution will
iterate over the entire list, regardless of an early hit.
But I don't think your second (as-pattern) solution for findBs
is ugly; I quite like it actually.
Cheers,
Paul
-----Original Message-----
From: Tom Nielsen [mailto:tanielsen at gmail.com]
Sent: Wed 12/11/2008 12:39
To: Paul Keir
Cc: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Searching for ADT patterns with elem
and find
somebody pointed out a few months back that list comprehensions
do this nicely:
containsTypeB ts = not $ null [x | (B x) <- ts]
no need for defining isTypeB.
not quite sure how you would write findBs :: [T]->[T]
succinctly; maybe
findBs ts = [b | b@(B _) <- ts]
or
findBs ts = [B x | (B x) <- ts]
both of them compile but the first is ugly and the second is
inefficient (Tags a new T for every hit).
Tom
2008/11/12 Paul Keir <pkeir at dcs.gla.ac.uk>:
> Hi All,
>
> If I have an ADT, say
>
> data T
> = A String Integer
> | B Double
> | C
> deriving(Eq)
>
> and I want to find if a list (ts) of type T contains an
element of subtype
> "B Double", must my "containsTypeX" function use a second
"isTypeX" function
> as follows:
>
> isTypeB :: T -> Bool
> isTypeB (B _) = True
> isTypeB _ = False
>
> containsTypeB :: [T] -> Bool
> containsTypeB ts = maybe False (\x -> True) (find isTypeB ts)
>
> I understand that while something like "find C ts" will work,
"find (isTypeB
> _) ts" will not, but is there no such thing as a pattern
combinator(?), or
> lambda that could help with this situation. I find I have many
individual
> "isTypeB" functions now.
>
> Regards,
> Paul
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer:
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081112/2e1b55e7/attachment.htm
More information about the Haskell-Cafe
mailing list