我有这个错误你们可以帮助我吗
Couldn't match expected type `Pixels' with actual type `[b0]'
In the return type of a call of `map'
In the expression: map (map cambio) b
In an equation for `negative':
negative (Pixels _ b)
= map (map cambio) b
where
cambio True = False
cambio False = True
Failed, modules loaded: none.
这是代码:
import qualified Graphics.HGL as G
import qualified Data.Bool
data Pixel = Pixel { on :: Bool }
data Pixels = Pixels { color :: G.Color, dots :: [[Pixel]] }
negative:: Pixels -> Pixels
negative (Pixels _ b) = map (map cambio) b
where
cambio True = False
cambio False = True
答案 0 :(得分:4)
您告诉编译器negative
返回类型Pixels
,但您正在使用map
,它返回一个列表。除此之外,您还试图将cambio
映射到Pixel
列表,但cambio
接受Bool
并返回,并且只是not
函数已重新实现。你可能想要更像
cambio :: Pixel -> Pixel
cambio (Pixel b) = Pixel (not b)
mapPixels :: (Pixel -> Pixel) -> Pixels -> Pixels
mapPixels f (Pixels c ds) = Pixels c (map (map f) ds)
negative :: Pixels -> Pixels
negative pixels = mapPixels cambio pixels
问题是您必须手动将[[Pixel]]
结果包装回Pixels
构造函数中,编译器不会为您执行此操作。由于您没有提供新的Color
值,编译器如何知道将该参数填充到构造函数中?