Greg O'Keefe:

> main =        readFile "input-file"                           >>= \ s ->
>       writeFile "output-file" (filter isAscii s)      >>
>       putStr "Filtering succesful\n"

[vs.]

> main =        readFile "input-file"                                   >>= 
>       \ s -> writeFile "output-file" (filter isAscii s)       >>
>       putStr "Filtering succesful\n"
> 
> My question is: is there some good reason why it is set out the way it
> is in the report?

The latter layout is certainly what one might expect 'normally', if this
were not monadic code.  As Martin Norbäck says, it's intended to convey
the idea that the value of the lambda-argument 's' "comes from" the
expression in the first line, and the monadic-bind operator >>= then
wraps it up so that it's then 'in scope' through the rest of the body
of the definition.  This way of thinking about it is even more explicit
in the corresponding do-notation:

> main = do  s <- readFile "input-file"
>            writeFile "output-file" (filter isAscii s)
>            putStr "Filtering succesful\n"


(Whether the style influenced the language feature, or the feature
the style I'm not certain, but I suspect the former.)

Hope that helps somewhat.

Cheers,
Alex.

Reply via email to