2011-04-03 40 views
1

直接點,類型聲明如下;Haskell:在抽象數據類型上使用地圖的問題

type Pnt = (Int, Int) 
type Image = Array Point Int 
type Pixels = [Int] 
type Dims = (Int, Int) 
type Block = (Point,Pixels) 

什麼我試圖做的是讓一個Image,並從該圖像在與寬度和長度Dims位置Pnt獲得像素的特定塊。只用一點就好,沒有問題或任何問題;

takeAblock :: Image -> Dims -> Pnt -> Block 
takeAblock i (w,h) (x,y) = ((x,y), [i!(u,v) | v <-[y..y + h - 1], u <- [x..x + w - 1]]) 

然而,當試圖讓多點,我發現自己停留在怎麼樣我認爲是正確執行,但是編譯器似乎並沒有跟我

takeManyBlocks :: Image -> Dims -> [Pnt] -> [Block] 
takeManyBlocks i d ps = takeAblock i d (map ps) 
             where ps (x,y) = x // Error 

而且同意錯誤是:

Couldn't match expected type `Pnt' 
     against inferred type `[(t, t1)] -> [t]' 
In the third argument of `takeAblock', namely `(map ps)' 
In the expression: takeAblock i d (map ps) 
In the definition of `takeAblock': 
    takeAblock i d ps 
       = takeAblock i d (map ps) 
       where 
        ps (x, y) = x 

我真的不能明白爲什麼牛逼他沒有工作,我甚至試圖map (*1) ps來檢查是否缺少一個規定的功能是問題,但沒有,編譯錯誤保持不變。我哪裏錯了?

+1

提示:map接受一個函數和一個列表。你通過了什麼? Owch,owch owch。 – Mikel 2011-04-03 03:57:07

+2

Owch,owch owch。請勿影響變量名稱(注意除了定義'ps'的where子句外,'takeManyBlocks'參數被命名爲'ps')。這使得甚至很難談論你在做什麼。在編譯器上使用警告以引起您的注意。 – 2011-04-03 04:43:20

回答

5

功能的缺乏確實是問題,但不是你想象的方式;類似map (*1) ps是一個無操作(至少ps數字列表)。你真正想要的是沿着map (takeAblock i d) ps;你想要在列表上映射的東西是map的第一個參數,而不是坐在另一邊的某個地方。

3

我想你想要的東西,如:

takeManyBlocks i d ps = [takeAblock i d p | p <- ps]