I often use expressions of the form maybe (return ()) someFunc someMaybe
.
Searching for a function Monad m => (a -> m ()) -> Maybe a -> m ()
on hoogle doesn't yield a specific result. Isn't there any library function for this?
Summary. Together with a functor called Either, Maybe is one of the workhorses of statically typed functional programming.
Map is not one of the defining properties of monads, however, because it's technically just a special case of FlatMap. A lifting function like Unit will wrap its object in a container, even if that object is itself the same type of container.
What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing . A richer error monad can be built using the Either type.
You can use mapM_
or forM_
from Data.Foldable for this. See also the recent discussion on the librariers mailing list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With