简体   繁体   中英

How to use map in Haskell with a variable and a list

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.

Partially apply the movePoint function (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM