有沒有辦法使用Haskell的「地圖」或類似的多個參數?在Haskell中應用於多個參數的地圖
即發現和其他點的列表給定的點(定義爲一個元組)之間的距離:
map distance (-3,-3) buildings
顯然,這是不行的,因爲它試圖「距離」映射到(-3,-3),其中距離預計兩個元組:
let distance pointA pointB = sqrt ((frst pointB - frst pointA) * (frst pointB - frst pointA) + (scnd pointB - scnd pointA) * (scnd pointB - scnd pointA))
距離取兩個點作爲參數:一個是(-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.
距離取兩個點作爲參數:一個是(-3,-3)在這個例子中,和一個從該列表「中選擇建築」。 012- (-3,-3)只是一個例子。這必須是一個變量;它不能被硬編碼到函數中。 – 2010-03-01 22:10:11
顯然「地圖(距離第一點)建築物」將工作,不是嗎?或者直截了當地說:「從建築物建築物=地圖(距離點)建築物」 – ondra 2010-03-01 22:21:29
當然,你可以用一個變量代替(-3,-3)。 – helium 2010-03-01 22:25:33