2014-05-09 33 views
0

我有這個錯誤你們可以幫助我,請哈斯克爾無法比擬預期型

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 

回答

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值,因此編譯器如何知道將該參數填充到構造函數中?