1

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 remains the same. Where am I going wrong?

2
  • 1
    Hint: map takes a function and a list. What are you passing it? Commented Apr 3, 2011 at 3:57
  • 2
    Owch, owch owch. Please don't shadow variable names (note the takeManyBlocks argument is named ps in addition to the where clause defining ps). This makes it hard to even talk about what you are doing. Use warnings on the compiler to bring these things to your attention. Commented Apr 3, 2011 at 4:43

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

I think you want something like:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.