[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>&nbsp;<BR>Yes my function operate only in a set-theory contest and \
your solution:<BR>&nbsp;<BR>subset xs ys = all (`elem` ys) \
xs<BR>&nbsp;<BR>is&nbsp;indeed more elegant than mine.<BR>&nbsp;<BR>Thanks again \
for&nbsp;your help.<BR>&nbsp;<BR>Luca.<BR>&nbsp;<BR>&gt; From: \
daniel.is.fischer@web.de<BR>&gt; To: beginners@haskell.org<BR>&gt; Subject: Re: \
[Haskell-beginners] subset - a little add<BR>&gt; Date: Fri, 29 Jan 2010 10:06:29 \
+0100<BR>&gt; CC: luca_ciciriello@hotmail.com<BR>&gt; <BR>&gt; Am Freitag 29 Januar \
2010 08:36:35 schrieb Luca Ciciriello:<BR>&gt; &gt; Just a little add to may previous \
mail.<BR>&gt; &gt;<BR>&gt; &gt; The solution I've found from myself is:<BR>&gt; \
&gt;<BR>&gt; &gt;<BR>&gt; &gt;<BR>&gt; &gt; subset :: [String] -&gt; [String] -&gt; \
Bool<BR>&gt; &gt; subset xs ys = and [elem x ys | x &lt;- xs]<BR>&gt; &gt;<BR>&gt; \
<BR>&gt; Variant:<BR>&gt; <BR>&gt; subset xs ys = all (`elem` ys) xs<BR>&gt; <BR>&gt; \
but is that really what you want? That says subset [1,1,1,1] [1] ~&gt; True.<BR>&gt; \
If you regard your lists as representatives of sets (as the name suggests), <BR>&gt; \
then that's correct, otherwise not.<BR>&gt; <BR>&gt; However, this is O(length xs * \
length ys). If you need it only for types <BR>&gt; belonging to Ord, a much better \
way is<BR>&gt; <BR>&gt; import qualified Data.Set as Set<BR>&gt; import Data.Set \
(fromList, isSubsetOf, ...)<BR>&gt; <BR>&gt; subset xs ys = fromList xs `isSubsetOf` \
fromList ys<BR>&gt; <BR>&gt; or, if you don't want to depend on Data.Set,<BR>&gt; \
<BR>&gt; subset xs ys = sort xs `isOrderedSublistOf` sort ys<BR>&gt; <BR>&gt; \
xxs@(x:xs) `isOrderedSublistOf` (y:ys)<BR>&gt; | x &lt; y = False<BR>&gt; | x == y = \
xs `isOrderedSublistOf` ys<BR>&gt; | otherwise = xxs `isOrderedSublistOf` ys<BR>&gt; \
[] `isOrderedSublistOf` _ = True<BR>&gt; _ `isOrderedSublistOf` [] = False<BR>&gt; \
<BR>&gt; &gt;<BR>&gt; &gt;<BR>&gt; &gt; My question is if exists a more elegant way \
to do that.<BR>&gt; &gt;<BR>&gt; &gt;<BR>&gt; &gt;<BR>&gt; &gt; Luca.<BR>&gt; \
<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