Tutorial by Examples

Maybe is used to represent possibly empty values - similar to null in other languages. Usually it is used as the output type of functions that can fail in some way. Consider the following function: halve :: Int -> Maybe Int halve x | even x = Just (x `div` 2) | odd x = Nothing Think ...
There is no way to get a value of type a out of an expression of type IO a and there shouldn't be. This is actually a large part of why monads are used to model IO. An expression of type IO a can be thought of as representing an action that can interact with the real world and, if executed, would r...
The lists form a monad. They have a monad instantiation equivalent to this one: instance Monad [] where return x = [x] xs >>= f = concat (map f xs) We can use them to emulate non-determinism in our computations. When we use xs >>= f, the function f :: a -> [b...
As of GHC 7.10, Applicative is a superclass of Monad (i.e., every type which is a Monad must also be an Applicative). All the methods of Applicative (pure, <*>) can be implemented in terms of methods of Monad (return, >>=). It is obvious that pure and return serve equivalent purposes, ...
You can wrap values into actions and pipe the result of one computation into another: return :: Monad m => a -> m a (>>=) :: Monad m => m a -> (a -> m b) -> m b However, the definition of a Monad doesn’t guarantee the existence of a function of type Monad m => m a -&...
do-notation is syntactic sugar for monads. Here are the rules: do x <- mx do x <- mx y <- my is equivalent to do y <- my ... ... do let a = b let a = b in ...
class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b The most important function for dealing with monads is the bind operator >>=: (>>=) :: m a -> (a -> m b) -> m b Think of m a as "an action with an a result". ...

Page 1 of 1