<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7653.38">
<TITLE>RE: [Haskell-cafe] Searching for ADT patterns with elem and find</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>Thanks Tom,<BR>
<BR>
That is indeed a very elegant solution; I too often forget about the wonders of list comprehension.<BR>
<BR>
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.<BR>
<BR>
But I don't think your second (as-pattern) solution for findBs is ugly; I quite like it actually.<BR>
<BR>
Cheers,<BR>
Paul<BR>
<BR>
<BR>
-----Original Message-----<BR>
From: Tom Nielsen [<A HREF="mailto:tanielsen@gmail.com">mailto:tanielsen@gmail.com</A>]<BR>
Sent: Wed 12/11/2008 12:39<BR>
To: Paul Keir<BR>
Cc: haskell-cafe@haskell.org<BR>
Subject: Re: [Haskell-cafe] Searching for ADT patterns with elem and find<BR>
<BR>
somebody pointed out a few months back that list comprehensions do this nicely:<BR>
<BR>
containsTypeB ts = not $ null [x | (B x) <- ts]<BR>
<BR>
no need for defining isTypeB.<BR>
<BR>
<BR>
not quite sure how you would write findBs :: [T]->[T] succinctly; maybe<BR>
<BR>
findBs ts = [b | b@(B _) <- ts]<BR>
<BR>
or<BR>
<BR>
findBs ts = [B x | (B x) <- ts]<BR>
<BR>
both of them compile but the first is ugly and the second is<BR>
inefficient (Tags a new T for every hit).<BR>
<BR>
<BR>
Tom<BR>
<BR>
<BR>
2008/11/12 Paul Keir <pkeir@dcs.gla.ac.uk>:<BR>
> Hi All,<BR>
><BR>
> If I have an ADT, say<BR>
><BR>
> data T<BR>
> = A String Integer<BR>
> | B Double<BR>
> | C<BR>
> deriving(Eq)<BR>
><BR>
> and I want to find if a list (ts) of type T contains an element of subtype<BR>
> "B Double", must my "containsTypeX" function use a second "isTypeX" function<BR>
> as follows:<BR>
><BR>
> isTypeB :: T -> Bool<BR>
> isTypeB (B _) = True<BR>
> isTypeB _ = False<BR>
><BR>
> containsTypeB :: [T] -> Bool<BR>
> containsTypeB ts = maybe False (\x -> True) (find isTypeB ts)<BR>
><BR>
> I understand that while something like "find C ts" will work, "find (isTypeB<BR>
> _) ts" will not, but is there no such thing as a pattern combinator(?), or<BR>
> lambda that could help with this situation. I find I have many individual<BR>
> "isTypeB" functions now.<BR>
><BR>
> Regards,<BR>
> Paul<BR>
><BR>
> _______________________________________________<BR>
> Haskell-Cafe mailing list<BR>
> Haskell-Cafe@haskell.org<BR>
> <A HREF="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</A><BR>
><BR>
><BR>
<BR>
</FONT>
</P>
</BODY>
</HTML>