<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>RE: [Haskell-cafe] Searching for ADT patterns with elem and find</TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3429" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff><FONT size=2><FONT 
face="Times New Roman"><FONT color=#000000><SPAN class=406415213-12112008>&gt; 
</SPAN>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.</FONT></FONT></FONT></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><FONT size=2><FONT face="Times New Roman" 
color=#000000></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=406415213-12112008><FONT 
face="Times New Roman" color=#000000 size=2>Nope, it will stop on the first one 
- Haskell is lazy like that :-)</FONT></SPAN></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=406415213-12112008><FONT size=2>Thanks, 
Neil</FONT></SPAN></DIV>
<DIV dir=ltr align=left><FONT size=2><BR></FONT></DIV></FONT>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff 
size=2>&nbsp;</DIV></FONT><BR>
<BLOCKQUOTE dir=ltr 
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
  <HR tabIndex=-1>
  <FONT face=Tahoma size=2><B>From:</B> haskell-cafe-bounces@haskell.org 
  [mailto:haskell-cafe-bounces@haskell.org] <B>On Behalf Of </B>Paul 
  Keir<BR><B>Sent:</B> 12 November 2008 1:45 pm<BR><B>To:</B> Tom 
  Nielsen<BR><B>Cc:</B> haskell-cafe@haskell.org<BR><B>Subject:</B> RE: 
  [Haskell-cafe] Searching for ADT patterns with elem and 
  find<BR></FONT><BR></DIV>
  <DIV></DIV><!-- 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) &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; "B 
  Double", must my "containsTypeX" function use a second "isTypeX" 
  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 "find C ts" will work, "find (isTypeB<BR>&gt; _) ts" 
  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; "isTypeB" 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></BLOCKQUOTE>
<P><pre wrap>==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================
</pre></P></BODY></HTML>