Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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.  best way to code this ([email protected])
   2. Re:  best way to code this (Lyndon Maydwell)
   3. Re:  best way to code this ([email protected])
   4. Re:  best way to code this (Alex Hammel)
   5. Re:  best way to code this (Chadda? Fouch?)


----------------------------------------------------------------------

Message: 1
Date: Sat, 30 May 2015 18:30:44 -0700
From: <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] best way to code this
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hi,

A simple example of something I was trying to do but it had some questions 
along the lines of "the best way to do this".

Given a list

  l = [a]

and an a-list

  alist = [ (a,b) ]

the idea is to find all the items in l which are in alist and then create a 
list [b]

so the overall function should be

 [a] -> [ (a,b) ] -> [b]

the solution is straightforward:

 l1 = filter (\x -> isJust (lookup x alist)) l
 l2 = map (\x -> fromJust (lookup x alist)) l1

`fromJust` used in the construction of l2 won't fail, because only the elements 
for which the lookup succeeded are in l1.

This would be something called `filterMap` but I couldn't find such a function 
in the list library, but it seems like there just has to be one defined in a 
library somewhere.

the above seems clumsy, i'm wondering how to make it "more pretty".

generally i was also wondering if the above construction is as inefficient as 
it looks because of the double-lookup, or would the compiler actually be able 
to optimize that code into something more efficient ?  this code is not being 
used on large sets of data so efficiency doesn't matter, I'm just curious.

Thanks,

Brian


------------------------------

Message: 2
Date: Sun, 31 May 2015 11:47:01 +1000
From: Lyndon Maydwell <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] best way to code this
Message-ID:
        <cam5qztxdyp1q86mlegsyeenohhe-xtam848_er17o+45b1b...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I think you're looking for `mapMaybe` :-)


 - Lyndon

On Sun, May 31, 2015 at 11:30 AM, <[email protected]> wrote:

> Hi,
>
> A simple example of something I was trying to do but it had some questions
> along the lines of "the best way to do this".
>
> Given a list
>
>   l = [a]
>
> and an a-list
>
>   alist = [ (a,b) ]
>
> the idea is to find all the items in l which are in alist and then create
> a list [b]
>
> so the overall function should be
>
>  [a] -> [ (a,b) ] -> [b]
>
> the solution is straightforward:
>
>  l1 = filter (\x -> isJust (lookup x alist)) l
>  l2 = map (\x -> fromJust (lookup x alist)) l1
>
> `fromJust` used in the construction of l2 won't fail, because only the
> elements for which the lookup succeeded are in l1.
>
> This would be something called `filterMap` but I couldn't find such a
> function in the list library, but it seems like there just has to be one
> defined in a library somewhere.
>
> the above seems clumsy, i'm wondering how to make it "more pretty".
>
> generally i was also wondering if the above construction is as inefficient
> as it looks because of the double-lookup, or would the compiler actually be
> able to optimize that code into something more efficient ?  this code is
> not being used on large sets of data so efficiency doesn't matter, I'm just
> curious.
>
> Thanks,
>
> Brian
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150531/78539146/attachment-0001.html>

------------------------------

Message: 3
Date: Sat, 30 May 2015 22:14:44 -0700
From: <[email protected]>
To: Lyndon Maydwell <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] best way to code this
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

On Sun, 31 May 2015 11:47:01 +1000
Lyndon Maydwell <[email protected]> wrote:

> I think you're looking for `mapMaybe` :-)
> 
> 

yes. yes i am. lol.

i even had the Maybe module open and just didn't get all the way to the end.

although after looking at my problem, i realized i need to save those elements 
of the list that weren't matched.

that makes it slightly more complicated.  but i did find partition which works 
nicely.

thanks!


Brian


------------------------------

Message: 4
Date: Sun, 31 May 2015 05:17:10 +0000
From: Alex Hammel <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] best way to code this
Message-ID:
        <CA+_xFepRwAGS1RmJBd9=nu_dnmwx2q4oykbfg7cjf3adza1...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

f as alist = [ b | (a, b) <- alist, a `elem` as ]

perhaps?

On Sat, 30 May 2015 6:47 pm Lyndon Maydwell <[email protected]> wrote:

> I think you're looking for `mapMaybe` :-)
>
>
>  - Lyndon
>
> On Sun, May 31, 2015 at 11:30 AM, <[email protected]> wrote:
>
>> Hi,
>>
>> A simple example of something I was trying to do but it had some
>> questions along the lines of "the best way to do this".
>>
>> Given a list
>>
>>   l = [a]
>>
>> and an a-list
>>
>>   alist = [ (a,b) ]
>>
>> the idea is to find all the items in l which are in alist and then create
>> a list [b]
>>
>> so the overall function should be
>>
>>  [a] -> [ (a,b) ] -> [b]
>>
>> the solution is straightforward:
>>
>>  l1 = filter (\x -> isJust (lookup x alist)) l
>>  l2 = map (\x -> fromJust (lookup x alist)) l1
>>
>> `fromJust` used in the construction of l2 won't fail, because only the
>> elements for which the lookup succeeded are in l1.
>>
>> This would be something called `filterMap` but I couldn't find such a
>> function in the list library, but it seems like there just has to be one
>> defined in a library somewhere.
>>
>> the above seems clumsy, i'm wondering how to make it "more pretty".
>>
>> generally i was also wondering if the above construction is as
>> inefficient as it looks because of the double-lookup, or would the compiler
>> actually be able to optimize that code into something more efficient ?
>> this code is not being used on large sets of data so efficiency doesn't
>> matter, I'm just curious.
>>
>> Thanks,
>>
>> Brian
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150531/88f207da/attachment-0001.html>

------------------------------

Message: 5
Date: Sun, 31 May 2015 09:47:50 +0000
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] best way to code this
Message-ID:
        <CANfjZRb9=l1xjbb6mqyxvyuucgd4qkyngk+vpef37ocm+-n...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Note that all proposed solutions are in O(n?) while this can be realized in
O(n log n) (sort both list then match them in order). It depends on your
use case if this is worthwhile.

-- 
Jeda?

Le dim. 31 mai 2015 ? 07:17, Alex Hammel <[email protected]> a ?crit :

> f as alist = [ b | (a, b) <- alist, a `elem` as ]
>
> perhaps?
>
> On Sat, 30 May 2015 6:47 pm Lyndon Maydwell <[email protected]> wrote:
>
>> I think you're looking for `mapMaybe` :-)
>>
>>
>>  - Lyndon
>>
>> On Sun, May 31, 2015 at 11:30 AM, <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> A simple example of something I was trying to do but it had some
>>> questions along the lines of "the best way to do this".
>>>
>>> Given a list
>>>
>>>   l = [a]
>>>
>>> and an a-list
>>>
>>>   alist = [ (a,b) ]
>>>
>>> the idea is to find all the items in l which are in alist and then
>>> create a list [b]
>>>
>>> so the overall function should be
>>>
>>>  [a] -> [ (a,b) ] -> [b]
>>>
>>> the solution is straightforward:
>>>
>>>  l1 = filter (\x -> isJust (lookup x alist)) l
>>>  l2 = map (\x -> fromJust (lookup x alist)) l1
>>>
>>> `fromJust` used in the construction of l2 won't fail, because only the
>>> elements for which the lookup succeeded are in l1.
>>>
>>> This would be something called `filterMap` but I couldn't find such a
>>> function in the list library, but it seems like there just has to be one
>>> defined in a library somewhere.
>>>
>>> the above seems clumsy, i'm wondering how to make it "more pretty".
>>>
>>> generally i was also wondering if the above construction is as
>>> inefficient as it looks because of the double-lookup, or would the compiler
>>> actually be able to optimize that code into something more efficient ?
>>> this code is not being used on large sets of data so efficiency doesn't
>>> matter, I'm just curious.
>>>
>>> Thanks,
>>>
>>> Brian
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150531/74c94103/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 83, Issue 60
*****************************************

Reply via email to