2014/02/17: Leaving the list monad

Monads describe computational effects, like exceptional values (Either) or mutable state (State). The list monad ([]) allows to write search in the style of a non-deterministic algorithm.

All the monads just mentioned have the property that you can "leave" them in the sense that you can use a computation in the monad to obtain an arbitrary type. For Either it's either, for State it's runState, but for list, the closest I could find the standard libraries is listToMaybe.


listToMaybe :: [a] -> Maybe a

It fits with the intuition of a non-deterministic algorithm: you either Just get an example, or Nothing. However, you don't really leave the monad, as you're just thrown into the next one. So I wonder, why I couldn't find the composition with maybe is the standard libraries yielding something like the runListHead recently added to Ganeti.


runListHead :: b -> (a -> b) -> [a] -> b
runListHead a f = maybe a f . listToMaybe