[Haskell-beginners] cascade of if statements

Daniel Trstenjak daniel.trstenjak at gmail.com
Sat Nov 3 12:13:02 CET 2012


Hi Rusi,

> We can do some algebraic simplification, using the law
> *if P  then True else Q  *is same as *P || Q* :
> 
> (<||>) m1 m2 = do
>    r1 <- m1
>    r2 <- m2
>    return (r1 || r2)
>
> 1> Are these equivalent or am I missing something?

Your operator doesn't behave in the same way, because r2 is computed,
even if r1 is true.

*Main> (putStrLn "m1" >> return True) <||> (putStrLn "m2" >> return True)
m1
m2
True

> 2> Any thumb rules on when to use Control.Monad and when to use
> Control.Applicative?

They're distinct type classes. So the question in this case is rather,
for which type class you need an operator.

When writing your own abstractions, than Applicative seems to be
more appropriate, if you have a more combinatorial abstraction,
like for parsers or HTML form fields.

But I'm also still trying to get a feeling for this.


Greetings,
Daniel



More information about the Beginners mailing list