[Haskell-beginners] Data.Map, types and debug

legajid legajid at free.fr
Thu Dec 31 06:53:05 EST 2009


Hi, 
studying IO with files, i got this tutorial program.
By the way, i discover the Data.Map module.
Several questions then come to my mind :
q1: why, in mapM_, make (sort (keys grades)) ? keys is already sorted, isn't it?
q2: why, in mapM_, have grades twice (in draw then keys).
	I wonder if writing only draw grades, one could then extract the s and g parts in the draw function (via keys and elems).
	From this, i have problems with the type of grades; which is it? The insert function, using insertWith should give 
	Map k a, but foldr seems to change this (couldn't match expected type [Map k a] against inferred type Map [String] a1
	on the mapM_ line .
q3: To solve the types problem, I tried to debug : i can get the types for s, marks and avg but for g, it says not in scope.
    How can i get this information ?
q4: I also tried to type the parameter in draw (draw (x::Map k a) = ...) but i got an error :
	Illegal signature in pattern
	use -XScopedTypeVariables to permit it
	Since this parameter is visibly not set by default in ghci, is it a good idea to set it ?

Thanks and happy new year.
Didier


    import Data.Char <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char>
    import Data.Maybe <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe>
    import Data.List
    import Data.Map hiding (map <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map>)
    import Text.Printf

    main = do
        src <- readFile <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:readFile> "grades"
        let pairs   = map <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map> (split.words <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:words>) (lines <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:lines> src)
        let grades  = foldr <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldr> insert empty pairs
        mapM_ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_> (draw grades) (sort (keys grades))
      where
        insert (s, g) = insertWith (++ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>) s [g]
        split [name,mark] = (name, read <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:read> mark)
 
    draw g s = printf "%s\t%s\tAverage: %f\n" s (show <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show> marks) avg
      where
        marks = findWithDefault (error <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:error> "No such student") s g
        avg   = sum <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum> marks / fromIntegral <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromIntegral> (length <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length> marks) :: Double <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double>




More information about the Beginners mailing list