Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: How to select items from one list depending on another
(Daniel Fischer)
2. Re: How to select items from one list depending on another
(Ozgur Akgun)
3. Re: How to select items from one list depending on another
(C K Kashyap)
4. Re: How to select items from one list depending on another
(Alexey Levan)
5. Re: How to select items from one list depending on another
(Magnus Therning)
6. Re: How to select items from one list depending on another
(C K Kashyap)
7. Re: How to select items from one list depending on another
(Brent Yorgey)
----------------------------------------------------------------------
Message: 1
Date: Wed, 10 Nov 2010 16:37:08 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Wednesday 10 November 2010 16:13:39, David Virebayre wrote:
> 2010/11/10 C K Kashyap <[email protected]>:
> > Hi,
> > I have this problem at hand -
> >
> > Given two lists
> >
> > list1 = [0,1,2,0,1,4]
> >
> > list2 = [1,2,3,4,5,6]
> > I need to take items from the second list only when the corresponding
> > item in list1 is non-zero.
>
> Another wayOne way to do it, although not shorter :
>
> list3 = catMaybes $ zipWith (\a b -> if a > 0 then Nothing else Just
> b) list1 list2
The function you zip with is
\a b -> guard (a /= 0) >> return b,
pointfree: (. return) . (>>) . guard . (/= 0)
using the MonadPlus instance of Maybe. One could use any MonadPlus with a
function [m a] -> [a] removing the mzeros, like catMaybes for Maybe, concat
for []:
import Control.Monad (guard)
list3 = concat $ zipWith ((. return) . (>>) . guard . (/= 0)) list1 list2
but I wouldn't say that's better than
list3 = map snd . filter ((/= 0) . fst) $ zip list1 list2
It's not shorter anyway.
>
> David.
------------------------------
Message: 2
Date: Wed, 10 Nov 2010 16:02:08 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: C K Kashyap <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
If only
ParallelListComp<http://hackage.haskell.org/trac/haskell-prime/wiki/ParallelListComp>supported
filters.
list3 = [ b | a <- list1
| b <- list2
, a /= 0
]
But it doesn't, so this is an entirely unnecessary post :)
Best,
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101110/a6e00b8e/attachment-0001.html
------------------------------
Message: 3
Date: Wed, 10 Nov 2010 21:37:26 +0530
From: C K Kashyap <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: Ozgur Akgun <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thank you folks ...
The best thing I like about Haskell is the wonderful mailing list :)
--
Regards,
Kashyap
------------------------------
Message: 4
Date: Wed, 10 Nov 2010 19:08:09 +0300
From: Alexey Levan <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: C K Kashyap <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
2010/11/10 C K Kashyap <[email protected]>:
> Hi,
> I have this problem at hand -
>
> Given two lists
>
> list1 = [0,1,2,0,1,4]
>
> list2 = [1,2,3,4,5,6]
>
> I need to take items from the second list only when the corresponding
> item in list1 is non-zero.
>
> list3 = map snd $ filter takeValid $ zip list1 list2 where
> Â Â Â Â Â Â Â Â Â Â Â takeValid (a,b) = a /= 0
>
> Is there a shorter way?
You can use list comprehenison and pattern matching here:
list3 = [xs | (0, xs) <- zip list1 list2]
------------------------------
Message: 5
Date: Wed, 10 Nov 2010 16:11:19 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: Alexey Levan <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Nov 10, 2010 at 16:08, Alexey Levan <[email protected]> wrote:
> 2010/11/10 C K Kashyap <[email protected]>:
>> Hi,
>> I have this problem at hand -
>>
>> Given two lists
>>
>> list1 = [0,1,2,0,1,4]
>>
>> list2 = [1,2,3,4,5,6]
>>
>> I need to take items from the second list only when the corresponding
>> item in list1 is non-zero.
>>
>> list3 = map snd $ filter takeValid $ zip list1 list2 where
>> Â Â Â Â Â Â Â Â Â Â Â takeValid (a,b) = a /= 0
>>
>> Is there a shorter way?
>
> You can use list comprehenison and pattern matching here:
>
> list3 = [xs | (0, xs) <- zip list1 list2]
Or, since the OP asked for the "opposite":
[b | (a, b) <- zip list1 list2, a /=0 ]
/M
--
Magnus Therning            (OpenPGP: 0xAB4DFBA4)
magnusï¼ therningï¼org     Jabber: magnusï¼ therningï¼org
http://therning.org/magnus     identi.ca|twitter: magthe
------------------------------
Message: 6
Date: Wed, 10 Nov 2010 21:45:43 +0530
From: C K Kashyap <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: Magnus Therning <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Aren't the list comprehension approach exponential in complexity -
whereas the others linear?
--
Regards,
Kashyap
------------------------------
Message: 7
Date: Wed, 10 Nov 2010 11:28:11 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
depending on another
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Nov 10, 2010 at 09:45:43PM +0530, C K Kashyap wrote:
> Aren't the list comprehension approach exponential in complexity -
> whereas the others linear?
No, they have exactly the same complexity. The list comprehension
simply desugars into something very similar to the other solutions
anyway.
-Brent
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 29, Issue 13
*****************************************