[Haskell-beginners] Syntactic sugar to insert into a Map?

Markus Läll markus.l2ll at gmail.com
Tue Jul 23 05:49:51 CEST 2013


Hi Martin,

I think given that integers are converted to fromInteger x and that
strings can be overloaded you could implement something like this with
a state or a writer monad:

mySet = do
    1 2
    3 4
    5 6

To do this you'll need to implement the appropriate classes. But I
guess it's more a hack towards getting what you want than to being
true to what a monad is.

On Mon, Jul 22, 2013 at 3:06 AM, Ozgur Akgun <ozgurakgun at gmail.com> wrote:
> Hi.
>
> On 21 July 2013 17:23, martin <martin.drautzburg at web.de> wrote:
>>
>> I just want to insert several key/value pairs, something like
>>
>> Map.empty
>> insert key1 val1
>> insert key2 val2
>> return theMap
>
>
> If you really want this syntax, what about using the writer monad:
>
> import Control.Monad.Writer
> import qualified Data.Map as M
>
> m = execWriter $ do
>     insert 1 "foo"
>     insert 2 "bar"
>     where insert k v = tell (M.singleton k v)
> -- m = fromList [(1,"foo"),(2,"bar")]
>
>
> Hope this helps,
> Ozgur
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Markus Läll




More information about the Beginners mailing list