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

Ozgur Akgun ozgurakgun at gmail.com
Mon Jul 22 09:06:20 CEST 2013


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,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130722/70430097/attachment.htm>

More information about the Beginners mailing list