【问题标题】:haskell Couldn't match expected typehaskell 无法匹配预期类型
【发布时间】:2014-05-09 14:24:56
【问题描述】:

我有这个错误请你们帮帮我

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

【问题讨论】:

    标签: haskell


    【解决方案1】:

    您告诉编译器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 值,编译器怎么知道用什么填充该参数给构造函数?

    【讨论】:

      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2020-09-05
      • 2020-03-17
      • 1970-01-01
      • 2016-07-18
      相关资源
      最近更新 更多