[Haskell-beginners] "module Main" from hitchhikers guide cd fit example

skip at pobox.com skip at pobox.com
Wed Jul 28 04:42:19 EDT 2010


I'm trying to work my way through the hitchhikers guide, knowing nothing
about Haskell (my day-to-day programming is in Python with a sprinkling of
C).  I've installed a binary distribution of Haskell Platform on my Mac.
GHCi reports version 6.12.3.  I'm currently in "3 Chapter 2: Parsing the
input".  I'm stuck on the module Main definition.  In hello.hs it works as
expected:

    module Main where
    main = putStrLn "Hello world!"

but in my nascent cd fitting code I get a parse error at the token "module"
("parse error on input `module'"):

    import Text.ParserCombinators.Parsec

    -- parseInput parses output of "du -sb", which consists of many lines,
    -- each of which describes single directory
    parseInput = 
      do dirs <- many dirAndSize
         eof
         return dirs

    -- Datatype Dir holds information about single directory - its size and name
    data Dir = Dir Int String deriving Show

    -- `dirAndSize` parses information about single directory, which is:
    -- a size in bytes (number), some spaces, then directory name, which extends till newline
    dirAndSize = 
      do size <- many1 digit
         spaces
         dir_name <- anyChar `manyTill` newline
         return (Dir (read size) dir_name)

    module Main where
      main =
          do input <- getContents
             putStrLn ("DEBUG: got input " ++ input)

When I comment out from "module Main where" to the end of the file
runhaskell complains "Not in scope: `main'", suggesting that the parse of
the code above it succeeded.  When I comment out all the code above "module
Main where" and run it, it displays what's on stdin.  It would appear there
is some sort of problem with the dirAndSize definition above that, but if
that was the case why didn't I get a parse error when removing the
definition of Main, unexpected EOF or something similar?

Note that at this point this is pretty much just a copy-and-paste exercise.
Perhaps there is some problem with copying indented 

Any help appreciated,

-- 
Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/


More information about the Beginners mailing list