I am learning about monads from the book 'Learn You a Haskell for Great Good!' by Miran Lipovaca. I am trying to understand the associativity law for monads. Essentially, the law states that when you have a chain of monadic function applications with >>=
, it shouldn't matter how they're nested.
The following code enables one to pass the result of a function of type a -> m b
to a function of type b -> m c
:
(<=<) :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
f <=< g = (\x -> g x >>= f)
However, for the example below:
ghci> let f x = [x, -x]
ghci> let g x = [x*3, x*2]
ghci> let h = f <=< g
ghci> h 3
[9, -9, 6, -6]
Are f x
and g x
both functions? It seems to be that they are lists with different values of x and not functions. How does the line let h = f <=< g
work in the above code? f
and g
have to be functions since they are used with <=<
but I am not sure what they are.