The error you posted is not coming from your definition of myMap
, it's coming from how you're using it. The type of the first myMap
is ([a] -> [a]) -> [a] -> [a]
, which does not match the type of Prelude.map
. In the second one you've swapped your variable names and also which one you're applying f
to. The compiler doesn't care what you name the arguments in your lambda being passed to foldr
, so foldr (\x acc -> f x : acc)
is identical to foldr (\foo bar -> f foo : bar)
. That may be what's tripping you up here.
The second one works because (to put it simply) it's correct. In the first you're applying f
to your accumulator list x
(even though you have a variable named acc
it's not your accumulator), so f
must take a list and return a list. In the second you're applying f
to each element, then prepending that to your accumulator list. If you had myMap (+1)
, it would have the type
myMap (+1) :: Num [a] => [a] -> [a]
Which says that you must pass it a list of values [a]
where [a]
implements Num
, and currently there is no instance for Num [a]
, nor will there ever be.
TL;DR: In the first one you're applying your mapped function to your accumulator list, in the second one you're applying the mapped function to each element.
map (+10) [1,2]
with both definitions and observe that the first one is not working, and possibly understand what's wrong with it. – chi