[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-beginners
Subject: [Haskell-beginners] subset - a little add
From: Luca Ciciriello <luca_ciciriello () hotmail ! com>
Date: 2010-01-29 10:01:36
Message-ID: SNT128-W4078FB7A2593E1F547AAA29A5B0 () phx ! gbl
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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@web.de
> To: beginners@haskell.org
> Subject: Re: [Haskell-beginners] subset - a little add
> Date: Fri, 29 Jan 2010 10:06:29 +0100
> CC: luca_ciciriello@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/
[Attachment #5 (text/html)]
<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
Thanks Daniel.<BR> <BR>Yes my function operate only in a set-theory contest and \
your solution:<BR> <BR>subset xs ys = all (`elem` ys) \
xs<BR> <BR>is indeed more elegant than mine.<BR> <BR>Thanks again \
for your help.<BR> <BR>Luca.<BR> <BR>> From: \
daniel.is.fischer@web.de<BR>> To: beginners@haskell.org<BR>> Subject: Re: \
[Haskell-beginners] subset - a little add<BR>> Date: Fri, 29 Jan 2010 10:06:29 \
+0100<BR>> CC: luca_ciciriello@hotmail.com<BR>> <BR>> Am Freitag 29 Januar \
2010 08:36:35 schrieb Luca Ciciriello:<BR>> > Just a little add to may previous \
mail.<BR>> ><BR>> > The solution I've found from myself is:<BR>> \
><BR>> ><BR>> ><BR>> > subset :: [String] -> [String] -> \
Bool<BR>> > subset xs ys = and [elem x ys | x <- xs]<BR>> ><BR>> \
<BR>> Variant:<BR>> <BR>> subset xs ys = all (`elem` ys) xs<BR>> <BR>> \
but is that really what you want? That says subset [1,1,1,1] [1] ~> True.<BR>> \
If you regard your lists as representatives of sets (as the name suggests), <BR>> \
then that's correct, otherwise not.<BR>> <BR>> However, this is O(length xs * \
length ys). If you need it only for types <BR>> belonging to Ord, a much better \
way is<BR>> <BR>> import qualified Data.Set as Set<BR>> import Data.Set \
(fromList, isSubsetOf, ...)<BR>> <BR>> subset xs ys = fromList xs `isSubsetOf` \
fromList ys<BR>> <BR>> or, if you don't want to depend on Data.Set,<BR>> \
<BR>> subset xs ys = sort xs `isOrderedSublistOf` sort ys<BR>> <BR>> \
xxs@(x:xs) `isOrderedSublistOf` (y:ys)<BR>> | x < y = False<BR>> | x == y = \
xs `isOrderedSublistOf` ys<BR>> | otherwise = xxs `isOrderedSublistOf` ys<BR>> \
[] `isOrderedSublistOf` _ = True<BR>> _ `isOrderedSublistOf` [] = False<BR>> \
<BR>> ><BR>> ><BR>> > My question is if exists a more elegant way \
to do that.<BR>> ><BR>> ><BR>> ><BR>> > Luca.<BR>> \
<BR><BR> <HR>
Not got a Hotmail account? <A \
href="http://clk.atdmt.com/UKM/go/197222280/direct/01/">Sign-up now - Free</A><BR> \
<br /><hr />Do you have a story that started on Hotmail? <a \
href='http://clk.atdmt.com/UKM/go/195013117/direct/01/' target='_new'>Tell us \
now</a></body> </html>
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic