It has turned out that many applications do not require monad functionality but only those of applicative functors. Monads allow you to run actions depending on the outcomes of earlier actions.
do text <- getLine if null text then putStrLn "You refuse to enter something?" else putStrLn ("You entered " ++ text)
This is obviously necessary is some cases, but in other cases it is disadvantageous.
Some advantages of applicative functors
- Code that uses only on the
Applicativeinterface are more general than ones uses the
Monadinterface, because there are more applicative functors than monads.
- Programming with
Applicativehas a more applicative/functional feel. Especially for newbies, it may encourage functional style even when programming with effects. Monad programming with
donotation encourages a more sequential & imperative style.
How to switch from monads
- Start using
liftM2, etc or
apwhere you can, in place of
- When you notice you're only using those monad methods, then import
liftA2, etc, and
(<*>). If your function signature was
Monad m => ..., change to
Applicative m => ...(and maybe rename