Malcolm writes:

> To be honest, I consider the first of these variants to be an ugly and
> confusing misuse of layout, and the second to be clear (if not the most
> beautiful way to code it).  The first seems to be trying to simulate a
> C-style `return', which is in reality not at all like the true monadic
> `return'.  I had to read it several times to understand what was
> intended.  More than ugly, I think it is dangerous, because of the
> possibility of confusing beginners who are new to both monads and
> layout.  Try this variation on the same theme:
> 
>       mcompare x y = 
>                do i <- x ==~ y 
>                   if i then return EQ else do i <- x <=~ y 
>                   if i then return LT else do return GT
> 
> This code now has a very serious bug!  If you use layout properly, as
> with strictly increasing indentation, this confusion is much less
> likely to arise.

I see your point, but this has an error anyway: the second "do" doesn't 
end with an expression, but with "i <- x <=~ y", which is a syntax 
error.

I often find I *do* want a C-style `return' in code; it's quite natural 
in the IO monad, for example, to test some (stateful) flag and only if 
it is true, do some stuff.  Thus:

possiblyAct :: IO ()
possiblyAct = do f <- checkFlag
                 if not f then return ()
                          else do {- stuff -}

If you do this more than once, you find yourself coding hard up against 
the right-hand edge of the edit window.  Obviously allowing the `else' 
to line up with the `if' would help a little here, but not 
completely... conceptually, {-stuff-} is in the same path of control as 
`f <- checkFlag'.

--KW 8-)

-- 
: Keith Wansbrough, MSc, BSc(Hons) (Auckland) ------------------------:
: PhD Student, Computer Laboratory, University of Cambridge, England. :
:  (and recently of the University of Glasgow, Scotland. [><] )       :
: Native of Antipodean Auckland, New Zealand: 174d47' E, 36d55' S.    :
: http://www.cl.cam.ac.uk/users/kw217/  mailto:[EMAIL PROTECTED]     :
:---------------------------------------------------------------------:




Reply via email to