if-then-else inside a "do"

Johannes Waldmann joe@isun.informatik.uni-leipzig.de
Wed, 30 Jan 2002 09:13:11 +0100 (MET)


> Forgot to mention this in my last email. I find myself writing a lot
> of if-then-elses in do notation, and most often the else branch is "return ()".
> This gets a bit cumbersome to write and messes up the code.

There's also the Monad library http://haskell.org/onlinelibrary/monad.html
that contains some combinators to make the code look better, for example

when             :: (Monad m) => Bool -> m () -> m ()
when p s         =  if p then s else return ()

can be used like this

foo = do
  ...
  when test $ bar
  ...

-- END facts, BEGIN opinion

I find myself writing a lot of do-groups.
since I think they are better than list comprehensions, because in 

    [ foo x y | x <- .., y <- .. ] 

you have to use identifiers x, y (textually) 
before their declaration, this is a bad habit.

Of course I know mathematicians do it all the time, as in

    f (g (.., y,) .. ) < h (x, z),      for all x

and I think this is bad style as well 
- check your favourite calculus textbook: with the typical formula, 
you're lucky if you find SOME of the variables declared 
(that is, quantified and typed) SOMEWHERE...

The effect is that when it comes to introduction to formal logics,
students just cannot believe that

    Quantifier variable formula

is the proper syntax in predicate logic. 
(Believe me, I've given these tutorials too often :-)

-- 
-- Johannes Waldmann ---- http://www.informatik.uni-leipzig.de/~joe/ --
-- joe@informatik.uni-leipzig.de -- phone/fax (+49) 341 9732 204/252 --