Reputation: 151
I'm planning to write am map function which essentially takes in a variable and a list and returns a list.
I've tried to use standard map but from what I've seen it's in the format "map function list" when here I'm trying to pass in another argument which is another point.
data Point = {xCoord :: Int,
yCoord :: Int}
movePoint :: Point -> Point -> Point
movePoint (Point x y) (Point xMove yMove)
= Point (x + xMove) (y + yMove)
// Add a "vector" to a list of points
movePoints :: [Point] -> Point -> [Point]
movePoints = error "Not yet"
For example, if I have a vector for example (2,2) and I have a list of Points such as [(-2,1),(0,0), (5,4) etc.] I want to use map to add (2,2) to all the Points in the list and return a list of points, I'm not sure how to do it. I'm a newbie when it comes to Haskell so any tips would be great.
Upvotes: 0
Views: 1291
Reputation: 48662
Partially apply the movePoint
function (i.e., call it with fewer arguments than it needs), like this:
movePoints pts vec = map (movePoint vec) pts
Doing so creates a new function that only needs one argument, the one that you didn't provide the first time. This is known as currying.
Upvotes: 7