[Haskell-beginners] subset - a little add
Luca Ciciriello
luca_ciciriello at hotmail.com
Fri Jan 29 05:01:36 EST 2010
Thanks Daniel.
Yes my function operate only in a set-theory contest and your solution:
subset xs ys = all (`elem` ys) xs
is indeed more elegant than mine.
Thanks again for your help.
Luca.
> From: daniel.is.fischer at web.de
> To: beginners at haskell.org
> Subject: Re: [Haskell-beginners] subset - a little add
> Date: Fri, 29 Jan 2010 10:06:29 +0100
> CC: luca_ciciriello at hotmail.com
>
> Am Freitag 29 Januar 2010 08:36:35 schrieb Luca Ciciriello:
> > Just a little add to may previous mail.
> >
> > The solution I've found from myself is:
> >
> >
> >
> > subset :: [String] -> [String] -> Bool
> > subset xs ys = and [elem x ys | x <- xs]
> >
>
> Variant:
>
> subset xs ys = all (`elem` ys) xs
>
> but is that really what you want? That says subset [1,1,1,1] [1] ~> True.
> If you regard your lists as representatives of sets (as the name suggests),
> then that's correct, otherwise not.
>
> However, this is O(length xs * length ys). If you need it only for types
> belonging to Ord, a much better way is
>
> import qualified Data.Set as Set
> import Data.Set (fromList, isSubsetOf, ...)
>
> subset xs ys = fromList xs `isSubsetOf` fromList ys
>
> or, if you don't want to depend on Data.Set,
>
> subset xs ys = sort xs `isOrderedSublistOf` sort ys
>
> xxs@(x:xs) `isOrderedSublistOf` (y:ys)
> | x < y = False
> | x == y = xs `isOrderedSublistOf` ys
> | otherwise = xxs `isOrderedSublistOf` ys
> [] `isOrderedSublistOf` _ = True
> _ `isOrderedSublistOf` [] = False
>
> >
> >
> > My question is if exists a more elegant way to do that.
> >
> >
> >
> > Luca.
>
Not got a Hotmail account? Sign-up now - Free
_________________________________________________________________
Send us your Hotmail stories and be featured in our newsletter
http://clk.atdmt.com/UKM/go/195013117/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100129/5941baf3/attachment.html
More information about the Beginners
mailing list