<!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 &quot;any&quot; (and staying with a separate &quot;isTypeB&quot;) 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) &lt;- ts]<BR>
<BR>
no need for defining isTypeB.<BR>
<BR>
<BR>
not quite sure how you would write findBs :: [T]-&gt;[T] succinctly; maybe<BR>
<BR>
findBs ts = [b | b@(B _) &lt;- ts]<BR>
<BR>
or<BR>
<BR>
findBs ts = [B x | (B x) &lt;- 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 &lt;pkeir@dcs.gla.ac.uk&gt;:<BR>
&gt; Hi All,<BR>
&gt;<BR>
&gt; If I have an ADT, say<BR>
&gt;<BR>
&gt; data T<BR>
&gt;&nbsp; = A String Integer<BR>
&gt;&nbsp; | B Double<BR>
&gt;&nbsp; | C<BR>
&gt;&nbsp; deriving(Eq)<BR>
&gt;<BR>
&gt; and I want to find if a list (ts) of type T contains an element of subtype<BR>
&gt; &quot;B Double&quot;, must my &quot;containsTypeX&quot; function use a second &quot;isTypeX&quot; function<BR>
&gt; as follows:<BR>
&gt;<BR>
&gt; isTypeB :: T -&gt; Bool<BR>
&gt; isTypeB (B _) = True<BR>
&gt; isTypeB _&nbsp;&nbsp;&nbsp;&nbsp; = False<BR>
&gt;<BR>
&gt; containsTypeB :: [T] -&gt; Bool<BR>
&gt; containsTypeB ts = maybe False (\x -&gt; True) (find isTypeB ts)<BR>
&gt;<BR>
&gt; I understand that while something like &quot;find C ts&quot; will work, &quot;find (isTypeB<BR>
&gt; _) ts&quot; will not, but is there no such thing as a pattern combinator(?), or<BR>
&gt; lambda that could help with this situation. I find I have many individual<BR>
&gt; &quot;isTypeB&quot; functions now.<BR>
&gt;<BR>
&gt; Regards,<BR>
&gt; Paul<BR>
&gt;<BR>
&gt; _______________________________________________<BR>
&gt; Haskell-Cafe mailing list<BR>
&gt; Haskell-Cafe@haskell.org<BR>
&gt; <A HREF="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</A><BR>
&gt;<BR>
&gt;<BR>
<BR>
</FONT>
</P>

</BODY>
</HTML>