How would you define map and filter using foldr in Haskell?

后端 未结 8 2509
一个人的身影
一个人的身影 2020-12-05 14:58

I\'m doing a bit of self study on functional languages (currently using Haskell). I came across a Haskell based assignment which requires defining map and filter in terms of

8条回答
  •  一个人的身影
    2020-12-05 15:50

    I wish I could just comment, but alas, I don't have enough karma.

    The other answers are all good ones, but I think the biggest confusion seems to be stemming from your use of x and xs.

    If you rewrote it as

    map'            :: (a -> b) -> [a] -> [b]
    map' f []       = []
    map' f (x:xs)   = foldr (\y ys -> (f y):ys) [] xs
    

    you would clearly see that x is not even mentioned on the right-hand side, so there's no way that it could be in the solution.

    Cheers

提交回复
热议问题