-1

I wonder, you say that flatMap is monad's bind method. The bind method takes a function that maps monad's contained item to another monad. This is how

option.flatMap(item => another option (f(item))) 

gives me another monad with f(item) inside. But, what does it mean applied to List? By strightforward extension, if you map every List item to a (list) monad, you get the whole list of lists. Why does bind flatten the outcome when applied to the list?

Little Alien
  • 1
  • 8
  • 22
  • "*to another monad*" should read "…maps the contained item to a container of the same monad" – Bergi Jun 18 '16 at 17:27

1 Answers1

1

Like the Option monad's bind does not return an option of an option, the List monad's bind does not return a list of lists. This joining is the whole point of monads - they are more than mere functors:

// Functor m
fmap :: (a -> b) -> m a -> m b
// Monad m
bind :: (a -> m b) -> m a -> m b // not m (m b)!

In the case of lists, this is done by flattening them.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375