0

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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
James Morgson
  • 131
  • 1
  • 1
  • 8
  • Just a comment and not an answer to your question: you mention adding a vector to a point (which makes sense) and then wrote a movePoint function that adds points to points. I know that Vector and Point are both pairs of Ints, but you'd still be better off defining a different data type for Vector and then writing movePoint :: Point -> Vector -> Point. – Marc Talbot Jun 16 '19 at 11:30
  • This may just be a typo, but your data declaration needs a constructor. So `data Point = Point {xCoord :: Int, yCoord :: Int}` – DarthFennec Jun 17 '19 at 16:45

1 Answers1

7

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.