I'm sure this is pretty obvious, but bear with me, I'm new to this stuff and it's not clicking. So I've, like many other people, been trying to get my head around Monads. I've gotten to the point where I'm comfortable with the >>= and return operators and so forth. But I feel like I won't really understand it until I get behind it and do some writing myself.
Consequently, I've been playing with trying to implement the bind >>= operator for the List as a composition of map and foldr. For example, [5,6,7,8] >>= (\x -> [x*5, x*6, x*7])
yields [25,30,35,30,36,42,35,42,49,40,48,56]
. This looks a lot like the composition of a map and a fold. But if I try something like foldr (++) [] . map
I get the obvious type error that map doesn't have type [a] -> [[a]]
as expected. Of course, if I instead use something like map (\x -> [x*5, x*6, x*7])
as the right argument to the composition operator it all works.
But it would be a hassle to specify a specific function every time; somehow the >>= operator behaves in a more general fashion. Is there a way to specify an argument by its type? Like, can I somehow tell map to only take functions of the type a -> [a]
in this composition? Do I need to write a function that has type (a -> [a]) -> [a] -> [[a]]
from scratch because there isn't really a way to narrow the map function to the type of function I want?
Also, please feel free to tell me I'm approaching this all wrong. I am still pretty new to this type of stuff. If so, please point me in the right direction though.