haskell无法匹配预期的类型

时间:2014-05-09 14:24:56

标签: haskell

我有这个错误你们可以帮助我吗

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

1 个答案:

答案 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值,编译器如何知道将该参数填充到构造函数中?