Malcolm Wallace wrote:
>
> | 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
> | with strictly increasing indentation iut would have to be something like.
> | 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
> | which is a bit uglier.
>
> 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:
The reason you didn't understand it the first time is because you have
never seen that idiom (using Monad return to simulate C-style return)
before. Once you understood the idiom, you were able to understand it.
For people like me who understand the idiom well, the code is quite
readable.
Consider
if ... then
{-code-}
else if .. then
{-code-}
else if .. then
{-code-}
else
{-code-}
Which I think almost everyone agrees is better than:
if ... then
{-code-}
else if .. then
{-code-}
else if .. then
{-code-}
else
{-code-}
--
Kevin Atkinson
[EMAIL PROTECTED]
http://metalab.unc.edu/kevina/