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:

mapping function on 2 input lists

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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -