Hi Unni,


>Why is it that when I am trying to redefine the getLine function of
>Prelude in
>the following way, I get an error message  "Last generator in do {...}
>must be
>an expression "                                                  
>
>getl   :: IO String
>getl    = do c <- getChar
> if c=='\n' then return ""
>    else do cs <- getl
>    return (c:cs)


You are bitten by the nasty offside rule :<> I used to like it, 
but I have way too many scars now  to appreciate it much
anymore. Try for instance try to replace every tab by two spaces 
in the prelude (which my editor does automatically) and then 
see what happens!

So I would write getl as follows:

getl :: IO String
getl = 
  do{ c <- getChar
       ; if c=='\n' 
       ; then do{ return "" }
       ; else do{ cs <- getl; return (c:cs) }
       }

A few extra braces and semicolons make it much cleare to
the human reader what you mean too.

Cheers,

Er

Reply via email to