The expressions f
and \x -> f x
do, for most purposes, mean the same thing. However, the scope of a lambda expression extends as far to the right as possible, i.e. m >>= (\x -> (f x >>= g))
.
If the types are m :: m a
, f :: a -> m b
, and g :: b -> m c
, then on the left we have (m >>= f) :: m b
, and on the right we have (\x -> f x >>= g) :: a -> m c
.
So, the difference between the two expressions is just which order the (>>=)
operations are performed, much like the expressions 1 + (2 + 3)
and (1 + 2) + 3
differ only in the order in which the additions are performed.
The monad laws require that, like addition, the answer should be the same for both.
\x -> f x >>= g
is the same asf >=> g
. (Kleisli fish) – Glee