我剛開始學習Haskell,並且在調整語言時遇到問題,例如在執行map
時更具體地嘗試執行類似下面的示例操作;Haskell Map功能實現問題
rotate :: Dimensions -> imgBlock -> [(imgBlock,Int)]
rotate d ((p, pix), s, t)
= zip [((p, f pix), s, t) | f <- transformate (fst d)] [0..7]
makeAllRotations :: Dimensions -> [imgBlock] -> [(imgBlock,Int)]
makeAllRotations d ib = map concat (rotate d ib) //Error points Here
凡
type imgBlock = (Block, Int, Int)
type Block = (Pnt, Pxl)
type Dimensions = (Int, Int)
這是錯誤的,我得到
asdf.hs:73:30:
Couldn't match expected type `(imgBlock, Int)'
with actual type `[a0]'
Expected type: [[a0]] -> (imgBlock, Int)
Actual type: [[a0]] -> [a0]
In the first argument of `map', namely `concat'
In the expression: map concat (rotate d ib)
之一,我發現自己很沮喪,努力適應這種新的編程「範式」,其中大部分的我設法做的事情是通過試驗和錯誤。我顯然不能正確理解map
,雖然我已經閱讀了關於這個website的文檔,但是所有的例子在map (2+) [1,2,3]
這樣的控制檯中都顯示出來,而不是在功能中使用它們。
我可以通過我的map
實現瞭解哪裏出錯。 THKS
一個好主意是命名中間值,使用'where'語法並且給所有事物賦予類型簽名。這迫使你思考(反對試驗和錯誤)。不要對haskell失去希望!如果你願意調整你的思維模式,它會報答你很好。 :) – Tarrasch 2011-04-27 19:21:32
我第二@ Tarrasch的建議。在學習Haskell時,如果需要的話,它可以幫助我們儘可能多地完成中間步驟:如果沒有其他任何東西,它就會澄清你的想法。最終,您可以省略越來越多的類型,並讓編譯器推斷它們。如果你寫了'map concat x where x :: ...'並試圖通過x的類型進行推理,那麼不用'map concat(rotate d ib)',你*可能已經發現了這個bug。或者學會解釋錯誤:它表示'concat'是實際類型'[[a0]] - > [a0]'(你知道),它不能與'[[a0]] - >' (imgBlock,Int)'。 – ShreevatsaR 2011-04-27 19:39:15
@Tarrasch,@ShreevatsaR;人們非常感謝你的建議和支持,這真的意味着世界在這樣的挫折時刻。我當然在實踐中提出了你的建議,顯然我需要找到一種方法來習慣像Haskell這樣的聲明性語言。再一次感謝 – pondigi 2011-04-27 20:16:39