Real World Haskell ch4 pp71-72
>> do x <- foo
>> y <- bar
>>
>> the y <- bar must be directly under the x on the previous line or it's a
>> syntax error, and the error you get from GHC is "the last statement of a
do
>> construct must be an expression"
>
> Huh, so this Haskell syntax actually prevents you from indenting something
that would be indented in any other language. Okay...

I didn't read it like this.  The x and y must be lined up because they are
both part of the same set of do-statements.  If the y line was a standalone
statement, then it could be indented as shown in this code segment.  But in
that case there's no need for a do, since the point of do is to order
statements in sequential time.

the following is a complete Haskell program, using do, which passes through
its input file to an output file.  The > specifies code lines, other lines
are considered comments. (I hope gmail preserves the spacing.)

Real World Haskell ch4 pp71-72

> 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 "error: need exactly two arguments"

Replace id with any function (of the same type)
to process the data.

> myFunction = id

Reply via email to