O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Create a new project working-with-traversable-foldable-map with the simple Stack template:
        stack new working-with-traversable-foldable-map simple
  1. Add dependency on the containers library in the build-depends subsection of the executable section:
        executable working-with-traversable-foldable-map
        hs-source-dirs:      src
        main-is:             Main.hs
        default-language:    Haskell2010
        build-depends:       base >= 4.7 && < 5
                       , containers
  1. Open src/Main.hs. We will add our source here. Import the Map module:
        import Data.Map.Strict as M hiding (foldr)
  1. Create a list of tuple of Int and String:
        a2z :: [(Int,String)]
        a2z = zipWith (\i c -> (i,c:[])) [1..] ['a'..'z']
  1. Create a map from the preceding list:
 imap :: M.Map Int String imap = M.fromList ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required