haskell - Writing a custom map function -
now there might in haskell libraries want. i'm enough of noob not know better , i'm trying write custom map function using tools know. type signature needs be
mymap :: (monad m) => (a -> b) -> [m a] -> [m b]
where mymap f as
returns list after applying f
each of value in each monad in as
.
my first attempt was
mymap f = map (\x x >>= f)
however, has type signature of
mymap :: (monad m) => (a -> m b) -> [m a] -> [m b]
this close need, can scream. need tips of how continue here. hope easy library function, i'm willing write own short function instead.
related question:
if turn (a -> b)
function in m -> m b
use map
itself. so, need this? hoogle quite sort of thing. doing search (a -> b) -> (m -> m b)
gives these results:
http://www.haskell.org/hoogle/?hoogle=%28a+-%3e+b%29+-%3e+%28m+a+-%3e+m+b%29
near top fmap
(which use functor
) , liftm
(which use monad
). either do, you're using monads, let's go liftm
. thus:
mymap :: monad m => (a -> b) -> [m a] -> [m b] mymap f = map (liftm f)
Comments
Post a Comment