<!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>>
</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> </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> </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> </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) <-
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></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>