[Haskell-cafe] MonadList?

Twan van Laarhoven twanvl at gmail.com
Wed Sep 13 18:49:43 EDT 2006

Michael Shulman wrote:
> The frequent occurence of "ListT $ return" in my code when I use the ListT
> monad transformer has made me wonder why there isn't a standard typeclass
> `MonadList', like those for the other monad transformers, encapsulating
> the essence of being a "list-like" monad -- in this case, the ability to
> select from a list of things.  I quickly wrote one for myself:
> class MonadList m where
>    option :: [a] -> m a

Another use for this class is for selecting a random option:

 > instance MonadList SomeMonadWithRandomness where
 >    option os = pos <- randomRM (0, length os - 1)
 >                return (os !! pos)

It can also be used for the Nondet monad described in 
http://haskell.org/hawiki/NonDeterminism, and as a replacement for the 
Parsec combinator 'choice' (which IMHO is a better name). Although msum 
might suffice in these cases.


More information about the Haskell-Cafe mailing list