"module Main" from hitchhikers guide cd fit example

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@pobox.com - http://www.smontanaro.net/

On Wed, Jul 28, 2010 at 1:42 AM,
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:
The "module Main were" has to be the first line in the file.

Johann> The "module Main were" has to be the first line in the file. Thanks for the quick reply. That suggests a bugfix is needed in the hitchhikers guide. In "Parsing the input" the first block of code contains an import state, definition of parseInput, declaration of data and definition of dirAndSize. Right after that it states: Just add those lines to the top of "cd-fit.hs". which is exactly what I did... I realize it's a wiki, so in theory I could edit the text, but at this stage of the game you probably don't want me crashing around there. Skip

On Wed, Jul 28, 2010 at 04:06:02AM -0500, skip@pobox.com wrote:
Johann> The "module Main were" has to be the first line in the file.
Thanks for the quick reply. That suggests a bugfix is needed in the hitchhikers guide. In "Parsing the input" the first block of code contains an import state, definition of parseInput, declaration of data and definition of dirAndSize. Right after that it states:
Just add those lines to the top of "cd-fit.hs".
which is exactly what I did... I realize it's a wiki, so in theory I could edit the text, but at this stage of the game you probably don't want me crashing around there.
Not at all, please edit it to something you would have found clear. I suppose it ought to say something like "at the top of the file, right after the module declaration" or something along those lines. -Brent

On Wednesday 28 July 2010 10:55:25, Johann Bach wrote:
On Wed, Jul 28, 2010 at 1:42 AM,
wrote: 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:
The "module Main were" has to be the first line in the file.
The first *code* line. Pragmas, comments and whitespace can preceed it. Generally, a module starts with -- LANGUAGE pragmas -- OPTIONS_TOOL pragmas -- Module comment -- Module Header -- Imports then come the definitions you make. Pragmas and imports appear of course only if needed. The module comment for haddocks is optional, but if you distribute a library, it's good practice to have one. The module header, module Foo (exportlist) where , is optional, if you omit it, "module Main (main) where" is assumed. If you omit the export list ("module Foo where"), all top-level bindings are exported.
participants (4)
-
Brent Yorgey
-
Daniel Fischer
-
Johann Bach
-
skip@pobox.com