[Haskell-cafe] return in Monad class necessary?
Chris Eidhof
chris at eidhof.nl
Tue Nov 27 04:45:19 EST 2007
On 27 nov 2007, at 10:14, Reinier Lamers wrote:
> Chris Eidhof wrote:
>
>> On 26 nov 2007, at 19:48, Henning Thielemann wrote:
>>
>>> I wonder whether it is a typical mistake of beginners
>>> to write 'return' within a do-block (that is, not at the end)
>>> and if it is possible to avoid this mistake by clever typing.
>>> In a proper monad 'return' can be fused with subsequent actions,
>>> and thus it is not necessary within a sequence of actions.
>>> However, although sensible, 'return' is also not required at the
>>> end of a block.
>>> Has someone already thought about a replacement for monads?
>>
>> I also made that mistake in the beginning, I used return instead
>> of lets. I don't think it's a big problem, most users will find
>> out once they've got some more experience, and it doesn't really
>> do any harm.
>
> It might be possible for the compiler to emit a warning when a
> return is used in the middle of a do block as the top level operator
> on a line. OTOH, that still wouldn't catch something like "when (x
> == 0) (return ())" which doesn't do what an imperative programmer
> expects.
Well, there are two things about the return:
First, some people want to use return just as an imperative programmer
would use it: to exit from a function. So the programmer doesn't
expect the commands after that return are executed.
Second, the problem I had was that I didn't know how to do
computations with the data I got from the monad, for example:
> main = do
> myLine <- getLine
> reversed <- return $ unwords $ reverse $ words myLine
> putStrLn reversed
Instead of the 3rd line I could have written
> let reversed = unwords $ reverse $ words myLine
This is another problem, but it doesn't affect the computation,
whereas the first problem is more serious.
-chris
More information about the Haskell-Cafe
mailing list