Trouble with formatting, Real World Haskell example

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081122/e2f2318b/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

Ed McCaffrey
On Sat, Nov 22, 2008 at 9:53 PM, Robert Kosara <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081122/e37ea330/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

Ed McCaffrey
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 <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


On Sat, Nov 22, 2008 at 10:41 PM, Ed McCaffrey <[hidden email]> wrote:

>
>
> On Sat, Nov 22, 2008 at 9:53 PM, Robert Kosara <[hidden email]> 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
>> [hidden email]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081122/8fa7b437/attachment-0001.htm
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

Steven Ashley
In reply to this post by Robert Kosara
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 <[hidden email]>:

> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

Kurt Hutchinson
On Sun, Nov 23, 2008 at 12:10 AM, Steven Ashley
<[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

Robert Kosara
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 <[hidden email]>wrote:

> On Sun, Nov 23, 2008 at 12:10 AM, Steven Ashley
> <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081124/89b1be3f/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Trouble with formatting, Real World Haskell example

Brent Yorgey-2
In reply to this post by Robert Kosara
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