2014-10-30 49 views
0
type Point = (Double, Double) 

flipsOneY :: Point -> (Int, Int) -> Point 
flipsOneY point (i, j) = ((fst point), ((fromIntegral j) - (snd point))) 

changeY :: [Point] -> (Int, Int) -> [Point] 
changeY xs (i, j) = map flipsOneY xs (i, j) 

我有一個點(x,y)的列表,我想要更改Y座標的值。當我嘗試編譯這個我收到此錯誤:使用'地圖'時輸入錯誤 - Haskell

Expr.hs:149:21: 
Couldn't match expected type `(Int, Int) -> Point' 
      with actual type `[(Int, Int) -> Point]' 
The function `map' is applied to three arguments, 
but its type `(Point -> (Int, Int) -> Point) 
       -> [Point] -> [(Int, Int) -> Point]' 
has only two 
In the expression: map flipsOneY xs (i, j) 
In an equation for `changeY': 
    changeY xs (i, j) = map flipsOneY xs (i, j) 

我猜我沒有正確使用地圖。任何提示解決方案的讚賞。 :-)

回答

3
type Point = (Double, Double) 

flipsOneY :: Point -> (Int, Int) -> Point 
flipsOneY point (i, j) = ((fst point), ((fromIntegral j) - (snd point))) 

changeY :: [Point] -> (Int, Int) -> [Point] 
changeY xs (i, j) = map flipsOneY xs (i, j) 

map需要2個參數,但你傳遞它三。

嘗試把括號圍繞flipOnesY調用,就像這樣:

changeY :: [Point] -> (Int, Int) -> [Point] 
changeY xs (i, j) = map (\ys -> flipsOneY ys (i, j)) xs 

這也說明你,你的論點爲了flipsOneY不是最優的。

+0

你能詳細說明「不是最優」的部分嗎?我認爲如何使它更優化的一個例子對於@Rewbert非常有用。 – bheklilr 2014-10-30 14:34:04

+0

這解決了我的問題,我一直在尋找一些額外的提示來了解更多。 :)如果我簡單地改變了參數的順序,還是有更大的缺陷需要考慮,那麼代碼會更好嗎? 我記得過去幾周看到你對我的線索有很多評論@bheklilr謝謝,這些周我學到了很多東西。幾乎完成我的第一個函數式編程課程:) – Rewbert 2014-10-30 15:07:32

+1

@Rewbert是的,如果您交換了「flipsOneY」的參數順序,那麼您可以將'changeY'定義爲'changeY xs ij = map(flipsOneY ij)xs',或者如果您已交換'changeY'中參數的順序可以用'changeY ij = map(flipsOneY ij)'來實現。 – bheklilr 2014-10-30 15:14:49