[英]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
. 试图做的是获取一个
Image
,并从该图像中获取位置Pnt
具有宽度和长度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 id (map ps) In the definition of `takeAblock': takeAblock id ps = takeAblock id (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. 我真的不明白为什么这行不通,我什至尝试了
map (*1) ps
来检查是否缺少指定的功能是问题所在,但是没有,编译错误保持不变。 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). 诸如
map (*1) ps
类的东西是无操作的(对于ps
,至少是数字列表)。 What you really want is something along the lines of map (takeAblock id) ps
; 您真正想要的是类似于
map (takeAblock id) 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. 您要映射到列表的对象是map的第一个参数,而不是位于
map
的另一侧。
我认为您想要类似的东西:
takeManyBlocks i d ps = [takeAblock i d p | p <- ps]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.