Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monad m => a -> [a -> m a] -> m a

Tags:

haskell

I'm new to Haskell, and I wonder if there's a better way to find out if one duplicates library functionality than Hoogle?

Case in point: I have a number of functions f :: Monad a => a -> m a that I want to chain together, like

f1234 x = (return x) >>= f1 >>= f2 >>= f3 >>= f4

But I'd rather write

chain :: Monad m => a -> [a -> m a] -> m a
chain = foldl (>>=) <$> return
f1234 = (flip chain) [f1, f2, f3, f4]

It seems very basic, does the base libary offer something equivalent to chain?

like image 929
pascal Avatar asked Oct 06 '12 00:10

pascal


People also ask

What is a monad in math?

A monad is a structure that is a lot like a monoid, but that lives in a bicategory rather than a monoidal category. In other words, the concept of a monad is a vertical categorification of that of a monoid. Monads are among the most pervasive structures in category theory and its applications.

What are the monad laws?

A proper monad must satisfy the three monad laws: left identity, right identity, and associativity. Together, left identity and right identity are know as simply identity.

How does the IO monad work?

The I/O monad contains primitives which build composite actions, a process similar to joining statements in sequential order using `;' in other languages. Thus the monad serves as the glue which binds together the actions in a program.

Is monad a Typeclass?

The Monad class As of GHC 7.10, the Applicative typeclass is a superclass of Monad , and the Functor typeclass is a superclass of Applicative . This means that all monads are applicatives, all applicatives are functors, and therefore all monads are also functors.


1 Answers

Hoogle's good for this, and definitely the right tool for finding a function with the same type.

Given that it's straightforward, and it's not turning up in any of the usual places, you may as well write it yourself as import it from some obscure module, partly because you won't import a whole load of other things.

(Aside: Some packages don't seem to be searchable from hoogle, so if you know the function, module or package name you're after and hoogle doesn't know, use hayoo.)

I'd like to plug

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)

from Control.Monad. It's the composition operator I always kept wanting till I found it. It's a more natural way to work with monads than >>= in my view.

You could even use it directly, it's so clear:

f1234 = f1 >=> f2 >=> f3 >=> f4

It shows up if you hoogle for (a -> m a) -> (a -> m a) -> (a -> m a), so a future strategy if you're looking for something that combines a list of something is to search for a function that combines two and use one of the fold functions.

Thus

chain' :: Monad m => [a -> m a] -> a -> m a
chain' = foldr (>=>) return

f1234 = chain' [f1,f2,f3,f4]

or

chain'' :: Monad m => a -> [a -> m a] -> m a
chain'' = flip $ foldr (>=>) return

if you prefer, but yours is fine anyway.

like image 83
AndrewC Avatar answered Sep 20 '22 13:09

AndrewC