开发者

Haskell: Problem using map on abstract data types

Straight to the point, the type declarations are as follow;

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

What am trying to do is to get an Image, and from that image obtain a particular block of pixels at position Pnt with a width and length Dims. When working with only one point is fine, no problems or whatsoever;

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]])

However when trying to get multiple points I'm finding myself stuck on how what I believed to be a correct implementation, however the compiler does not seem to agree with me

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

And the error is the following:

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

I really cant understand why this is not working, I even tried map (*1) ps to check whether the lack of a stated function was the problem, but nothing, the compile error re开发者_高级运维mains the same. Where am I going wrong?


The lack of the function is indeed the problem, but not the way you seem to think; something like map (*1) ps is a no-op (for ps a list of numbers, at least). What you really want is something along the lines of map (takeAblock i d) ps; the thing you want to map over the list is the first parameter to map, not sitting somewhere on the other side of it.


I think you want something like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜