Trouble with formatting, Real World Haskell example

I'm having some trouble getting the InteractWith example from Real World Haskell's Chapter 4 to compile. When I use spaces (like below), it says "Interact.hs:10:30: Empty 'do' construct." When I use tabs (4 spaces per tab), I get "Interact.hs:16:13: parse error on input `='." In either case, I don't see the problem. Any help would be greatly appreciated. I'm also confused about the formatting of the example in the book (page 72). The last line ("myFunction = id") seems to be indented between the where and the next line, why is that? Is that simply a layout problem? To me, it seems it should be on the same level as the where. Below is my program: -- Interact.hs, simple filter in Haskell import System.Environment (getArgs) interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input) main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile" myFunction = id Regards, Robert

On Sat, Nov 22, 2008 at 9:53 PM, Robert Kosara
I'm having some trouble getting the InteractWith example from Real World Haskell's Chapter 4 to compile. When I use spaces (like below), it says "Interact.hs:10:30: Empty 'do' construct." When I use tabs (4 spaces per tab), I get "Interact.hs:16:13: parse error on input `='." In either case, I don't see the problem. Any help would be greatly appreciated. I'm also confused about the formatting of the example in the book (page 72). The last line ("myFunction = id") seems to be indented between the where and the next line, why is that? Is that simply a layout problem? To me, it seems it should be on the same level as the where.
Below is my program:
-- Interact.hs, simple filter in Haskell
import System.Environment (getArgs)
interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input)
main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile"
myFunction = id
Regards,
Robert
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I hope the spacing is preserved by my email client; here's a formatting that
compiles:
import System.Environment (getArgs)
interactWith function inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (function input)
main = mainWith myFunction
where mainWith function = do
args <- getArgs
case args of
[input, output] -> interactWith function input output
_ -> putStrLn "Usage: Interact inputFile outputFile"
myFunction = id
On Sat, Nov 22, 2008 at 9:53 PM, Robert Kosara
I'm having some trouble getting the InteractWith example from Real World Haskell's Chapter 4 to compile. When I use spaces (like below), it says "Interact.hs:10:30: Empty 'do' construct." When I use tabs (4 spaces per tab), I get "Interact.hs:16:13: parse error on input `='." In either case, I don't see the problem. Any help would be greatly appreciated. I'm also confused about the formatting of the example in the book (page 72). The last line ("myFunction = id") seems to be indented between the where and the next line, why is that? Is that simply a layout problem? To me, it seems it should be on the same level as the where.
Below is my program:
-- Interact.hs, simple filter in Haskell
import System.Environment (getArgs)
interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input)
main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile"
myFunction = id
Regards,
Robert
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
On Sat, Nov 22, 2008 at 10:41 PM, Ed McCaffrey
On Sat, Nov 22, 2008 at 9:53 PM, Robert Kosara
wrote: I'm having some trouble getting the InteractWith example from Real World Haskell's Chapter 4 to compile. When I use spaces (like below), it says "Interact.hs:10:30: Empty 'do' construct." When I use tabs (4 spaces per tab), I get "Interact.hs:16:13: parse error on input `='." In either case, I don't see the problem. Any help would be greatly appreciated. I'm also confused about the formatting of the example in the book (page 72). The last line ("myFunction = id") seems to be indented between the where and the next line, why is that? Is that simply a layout problem? To me, it seems it should be on the same level as the where.
Below is my program:
-- Interact.hs, simple filter in Haskell
import System.Environment (getArgs)
interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input)
main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile"
myFunction = id
Regards,
Robert
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi Robert,
The contents of the do block must be indented at-least as much as
mainWith on the previous line.
For instance:
import System.Environment (getArgs)
interactWith function inputFile outputFile = do
....input <- readFile inputFile
....writeFile outputFile (function input)
main = mainWith myFunction
....where mainWith function = do
....................args <- getArgs
....................case args of
..............................[input, output] -> interactWith function
input output
.............................._ -> putStrLn "Usage: Interact inputFile
outputFile"
....................myFunction = id
The haskell wikibook has some more detailed information of
http://en.wikibooks.org/wiki/Haskell/Indentation
Regards,
--Steven
2008/11/23 Robert Kosara
I'm having some trouble getting the InteractWith example from Real World Haskell's Chapter 4 to compile. When I use spaces (like below), it says "Interact.hs:10:30: Empty 'do' construct." When I use tabs (4 spaces per tab), I get "Interact.hs:16:13: parse error on input `='." In either case, I don't see the problem. Any help would be greatly appreciated. I'm also confused about the formatting of the example in the book (page 72). The last line ("myFunction = id") seems to be indented between the where and the next line, why is that? Is that simply a layout problem? To me, it seems it should be on the same level as the where. Below is my program: -- Interact.hs, simple filter in Haskell import System.Environment (getArgs) interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input) main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile" myFunction = id
Regards, Robert
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 23, 2008 at 12:10 AM, Steven Ashley
The contents of the do block must be indented at-least as much as mainWith on the previous line.
main = mainWith myFunction ....where mainWith function = do ....................args <- getArgs
This is the reason that I format 'where' clauses like so: main = mainWith myFunction ....where ....mainWith f = do ........args <- getArgs etc. I think one more line of vertical space is worth all of the horizontal space you end up saving. Kurt

Kurt,
that's a good idea, that also groups the elements in the where much
better.
Thanks everybody for your responses (I don't think I've responded to the
list so far).
Regards,
Robert
On Mon, Nov 24, 2008 at 9:23 AM, Kurt Hutchinson
On Sun, Nov 23, 2008 at 12:10 AM, Steven Ashley
wrote: The contents of the do block must be indented at-least as much as mainWith on the previous line.
main = mainWith myFunction ....where mainWith function = do ....................args <- getArgs
This is the reason that I format 'where' clauses like so:
main = mainWith myFunction ....where ....mainWith f = do ........args <- getArgs
etc. I think one more line of vertical space is worth all of the horizontal space you end up saving.
Kurt _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sat, Nov 22, 2008 at 09:53:37PM -0500, Robert Kosara wrote:
main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile"
myFunction = id
I think the point is that this 'where' block is supposed to introduce two definitions: one for 'mainWith' and one for 'myFunction'. The 'myFunction = id' should not be part of the do-block (it wouldn't make sense, and isn't even syntactically correct). The layout rule is this: the column of the first thing following the 'do' determines the indentation for the rest of the do-block. The first line which is indented *less* than that is the first line following the end of the do-block. So: do foo bar baz not part of the do-block! or: mainWith function = do start of the do-block (doesn't necessarily need to be indented past 'mainWith') another line in the do-block this is not part of the do-block I would indent the code like so:
main = mainWith myFunction where mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "Usage: Interact inputFile outputFile"
myFunction = id
Also, you should never use tabs -- it's hard to predict how they will be interpreted by the layout rule, and haskell files using tabs are also non-portable, in the sense that if you send it to someone else they may have different tab settings, etc. It should be possible to tell your favorite editor to automatically convert tabs into spaces. -Brent
participants (5)
-
Brent Yorgey
-
Ed McCaffrey
-
Kurt Hutchinson
-
Robert Kosara
-
Steven Ashley