Thanks Tom,
That is indeed a very elegant solution; I too often forget about the wonders of
list comprehension.
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
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.
Nope, it will stop on the first one - Haskell is lazy like that :-)
Thanks, Neil
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
Hi Paul,
maybe False (\x - True) (find isTypeB ts)
This can be more neatly expressed as:
isJust (find isTypeB ts)
But your entire thing can be expressed as:
containsTypeB ts = any isTypeB ts
I recommend reading through the Prelude interface and the List
interface, it has many useful
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
Thanks Neil,
Great. I hadn't noticed isJust, and I'd forgotten any. Actually I was
browsing Prelude just the other day and picked up zipWith f as bs as a
replacement for map f $ zip as bs.
Cheers, Paul
-Original Message-
From: Mitchell, Neil [mailto:[EMAIL PROTECTED]
Sent: Wed
somebody pointed out a few months back that list comprehensions do this nicely:
containsTypeB ts = not $ null [x | (B x) - ts]
no need for defining isTypeB.
not quite sure how you would write findBs :: [T]-[T] succinctly; maybe
findBs ts = [b | b@(B _) - ts]
or
findBs ts = [B x | (B x) -
On Wed, Nov 12, 2008 at 10:20 AM, Derek Elkins [EMAIL PROTECTED] wrote:
In addition to what others have said, I recommend using functions like
isTypeB :: T - Maybe Double
isTypeB (B d) = Just d
isTypeB _ = Nothing
You can recover the Bool version of isTypeB just by post-composing with
Paul Keir wrote:
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
On Wed, 2008-11-12 at 10:09 +, Paul Keir wrote:
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
10 matches
Mail list logo