Jas*_*n B 9 haskell functional-programming
有没有办法使用Haskell的"地图"或类似的多个参数?
即找到给定点(定义为元组)与其他点列表之间的距离:
map distance (-3,-3) buildings
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用,因为它试图将"距离"映射到(-3,-3),其中距离需要两个元组:
let distance pointA pointB = sqrt ( (frst pointB - frst pointA) * (frst pointB - frst pointA) + (scnd pointB - scnd pointA) * (scnd pointB - scnd pointA) )
Run Code Online (Sandbox Code Playgroud)
距离取两个点作为参数:在这个例子中,一个是(-3,-3),一个是从"建筑物"列表中选择的.
(-3,-3)只是一个例子.这必须是一个变量; 它不能硬编码到函数中.
也许这会更有意义:
buildings = [(3,-2),(2,1),(5,3),(4,3),(4,-1)]
firstDiff pointA pointB = subtract ( fst pointA ) ( fst pointB )
secondDiff pointA pointB = subtract ( snd pointA ) ( snd pointB )
distance pointA pointB = sqrt ( (firstDiff pointA pointB) * (firstDiff pointA pointB) + (secondDiff pointA pointB) * (secondDiff pointA pointB))
--- What I need to happen here is a list "score" to be created by taking all distances from a point in a list lPoints to a point in list buildings.
Run Code Online (Sandbox Code Playgroud)
eph*_*ent 21
allDistances src dests = map (\point -> distance src point) dests
allDistances src dests = map (distance src) dests
allDistances src = map (distance src)
allDistances = map . distance
Run Code Online (Sandbox Code Playgroud)
ja.*_*ja. 13
你要:
map (distance (-3, -3)) buildings
Run Code Online (Sandbox Code Playgroud)
是的
map f buildings
where f = distance (-3, -3)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17135 次 |
| 最近记录: |