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. Re:  Add parenthesis to Sin/Cos expressions
      (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   2.  mapKeysMaybe (Dennis Raddle)
   3. Re:  mapKeysMaybe (Lyndon Maydwell)
   4. Re:  mapKeysMaybe (Lyndon Maydwell)
   5. Re:  mapKeysMaybe (Lyndon Maydwell)


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

Message: 1
Date: Thu, 5 Nov 2015 22:46:43 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Add parenthesis to Sin/Cos
        expressions
Message-ID:
        <cajbew8mvjapegzfzhu5rhzo5-ngrz69lusstof6bj6tzfqf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The spirit of declarative programming lies in the fact that you try to just
tell the compiler what something means. Instead of thinking about keeping
track of the numbers of parentheses, and then applying them afterwards, I
would suggest you create a function to do the same job:

    bracketed :: String -> String
    bracketed s = "(" ++ s ++ ")"

This would allow you to remove the numeric argument, and also write cleaner
code.

The second thing that you can improve is the pattern matching. You can use
isPrefixOf
<http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html#v:isPrefixOf>
to
check functions, or use a take inside a case statement to get cleaner
handling of different cases.

    case take 3 str of
      "sin" -> ...
      "cos" -> ...
       _ -> ...

This would also let you debug your code more easily, just as you require.

On 4 November 2015 at 14:51, Imants Cekusins <[email protected]> wrote:

> > a function that is supposed to add parenthesis to sin/cos expressions.
> a) Sin 5 + 3        ->    Sin(5) + 3
> b) Sin Cos (5+3)   ->    Sin (Cos(5+3))
> c) Sin Cos 5 * 3   ->    Sin(Cos(5)) * 3
>
> are you looking for textual representation or actual Haskell code which
> runs?
> if actual code, these examples may be re-written like this:
> a) sin 5 + 3
> b) sin $ cos $ 5 + 3
> c) sin (cos 5) * 3
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151105/a5795810/attachment-0001.html>

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

Message: 2
Date: Thu, 5 Nov 2015 23:29:36 -0800
From: Dennis Raddle <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] mapKeysMaybe
Message-ID:
        <cakxlvopg4uelsvrvq7z7j_i7wrlezmimtt+sx3abtwoej0h...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Can someone give me a clever way to implement mapKeysMaybe, defined as
follows?

Ord k => (k -> Maybe k) -> Map k a -> Map k a

where the transformed keys that evaluate to Just are the only ones kept?

I am thinking this will probably help me understand a new type, like
traversable or something.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151105/3a0857ff/attachment-0001.html>

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

Message: 3
Date: Fri, 6 Nov 2015 18:43:47 +1100
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] mapKeysMaybe
Message-ID:
        <CAM5QZtxg089PfHuD9kxd4AhekRpJCNK69dd-p=H_q29j0Zi=1...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Well, this might not be the most efficient, but:

mapKeysMaybe f = Data.Map.mapMaybeWithKey (const . f)


What do you reckon?

On Fri, Nov 6, 2015 at 6:29 PM, Dennis Raddle <[email protected]>
wrote:

> Can someone give me a clever way to implement mapKeysMaybe, defined as
> follows?
>
> Ord k => (k -> Maybe k) -> Map k a -> Map k a
>
> where the transformed keys that evaluate to Just are the only ones kept?
>
> I am thinking this will probably help me understand a new type, like
> traversable or something.
>
>
> _______________________________________________
> 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/20151106/39d4a7c3/attachment-0001.html>

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

Message: 4
Date: Fri, 6 Nov 2015 18:46:40 +1100
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] mapKeysMaybe
Message-ID:
        <cam5qztzwspo+buoobx8c0wufmo+ptuv_pgy7mf4bodd9f_a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Oops disregard that. The mapMaybeWithKey doesn't quite do what I expected.

On Fri, Nov 6, 2015 at 6:43 PM, Lyndon Maydwell <[email protected]> wrote:

> Well, this might not be the most efficient, but:
>
> mapKeysMaybe f = Data.Map.mapMaybeWithKey (const . f)
>
>
> What do you reckon?
>
> On Fri, Nov 6, 2015 at 6:29 PM, Dennis Raddle <[email protected]>
> wrote:
>
>> Can someone give me a clever way to implement mapKeysMaybe, defined as
>> follows?
>>
>> Ord k => (k -> Maybe k) -> Map k a -> Map k a
>>
>> where the transformed keys that evaluate to Just are the only ones kept?
>>
>> I am thinking this will probably help me understand a new type, like
>> traversable or something.
>>
>>
>> _______________________________________________
>> 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/20151106/e1c078d0/attachment-0001.html>

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

Message: 5
Date: Fri, 6 Nov 2015 19:19:03 +1100
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] mapKeysMaybe
Message-ID:
        <CAM5QZtzcqr2rG1m5ni-hTzMSpgW-5G6+=dal9ycvvd4uzp5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Here's a more correct solution, unfortunately it's not as nice.


{-# LANGUAGE TupleSections #-}

import Data.Map hiding (mapMaybe)
import Data.Maybe

mkm :: Ord k => (t -> Maybe k) -> Map t a -> Map k a
mkm f = fromList . mapMaybe (\(k,v) -> fmap (,v) (f k)) . toList


On Fri, Nov 6, 2015 at 6:46 PM, Lyndon Maydwell <[email protected]> wrote:

> Oops disregard that. The mapMaybeWithKey doesn't quite do what I expected.
>
> On Fri, Nov 6, 2015 at 6:43 PM, Lyndon Maydwell <[email protected]>
> wrote:
>
>> Well, this might not be the most efficient, but:
>>
>> mapKeysMaybe f = Data.Map.mapMaybeWithKey (const . f)
>>
>>
>> What do you reckon?
>>
>> On Fri, Nov 6, 2015 at 6:29 PM, Dennis Raddle <[email protected]>
>> wrote:
>>
>>> Can someone give me a clever way to implement mapKeysMaybe, defined as
>>> follows?
>>>
>>> Ord k => (k -> Maybe k) -> Map k a -> Map k a
>>>
>>> where the transformed keys that evaluate to Just are the only ones kept?
>>>
>>> I am thinking this will probably help me understand a new type, like
>>> traversable or something.
>>>
>>>
>>> _______________________________________________
>>> 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/20151106/023a08af/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 89, Issue 6
****************************************

Reply via email to