I've recently began studying Haskell and in one of my assignments I've got an exercise which asks to define the map function in terms of foldr, and I can't for the life of me figure out how to do this. I've searched stack overflow for solutions and came across this one:
How would you define map and filter using foldr in Haskell?
However here the solution involves using lambdas, which I haven't covered yet and I assume that because of this the exercise should be doable without lambdas (although you never know).
I guess what confuses me the most is that map takes in a unary function (e.g. +1), whereas foldr takes a binary function (e.g. +), and I don't really see how to make these two work together.
id :: [a] -> [a]withfoldr. Then remember thatid :: [a] -> [a]is the same thing asmap (id :: a -> a), and figure out where you need to apply the extra function. - Carlleft_fold f z xs = let rs = z : zipWith f xs rs in last rs; zipWith is a map:zipWith f a b = map (uncurry f) $ zip a b(although,lastis kind of a fold...(?)). But, ignoring that,rev xs = left_fold (flip (:)) [] xs(reverse is a left fold); and thenright_fold f z xs = let rs = z : zipWith f (rev xs) rs in last rs. cf. stackoverflow.com/a/11951590/849891. - Will Ness