[Haskell-cafe] Searching for ADT patterns with elem and
find
Mitchell, Neil
neil.mitchell.2 at credit-suisse.com
Wed Nov 12 08:45:05 EST 2008
> containsTypeB ts = not $ null [x | (B x) <- ts]
No need for the brackets on the left of the <-:
not $ null [x | B x <- ts]
> 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).
If optimised does it really create a new B tag for each node? That seems
like something that could be optimised away by the compiler. Either way,
the difference is probably minimal. (There may be possible space leaks
if the compiler did share the B, but I can't think of any off hand)
Thanks
Neil
>
>
> 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
> >
> >
> _______________________________________________
> 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
==============================================================================
More information about the Haskell-Cafe
mailing list