【发布时间】:2018-05-14 05:04:31
【问题描述】:
data PPMImage a = PPMImage {width :: Integer,
height :: Integer,
magicNumber :: Integer,
maxColor :: Integer,
pixels :: [a]} deriving (Show)
instance Functor PPMImage where
fmap f (PPMImage w h m c p) = f PPMImage w h m c (f p)
我想我了解函子的整个包装和展开方面 - 用户 MCH 提供的this link 帮助很大。
函数和列表已经是函子,但是我定义的这个PPMImage没有默认函子实例。我正在尝试定义一个仅应用于PPMImage 的(像素)数组的函数,但我不断收到此错误:
Couldn't match expected type ‘[b]’ with actual type ‘b’
‘b’ is a rigid type variable bound by
the type signature for:
fmap :: forall a b. (a -> b) -> PPMImage a -> PPMImage b
at New.hs:13:5-8
• In the fifth argument of ‘PPMImage’, namely ‘(f p)’
In the expression: PPMImage w h m c (f p)
In an equation for ‘fmap’:
fmap f (PPMImage w h m c p) = PPMImage w h m c (f p)
• Relevant bindings include
f :: a -> b (bound at New.hs:13:10)
fmap :: (a -> b) -> PPMImage a -> PPMImage b (bound at New.hs:13:5)
我不明白为什么会这样,这个函子不就是解开原来的PPMImage,然后应用函数f,然后重新包装成一个新的PPMImage吗?
【问题讨论】:
-
嗨@hdizzle!如果您收到的答案令人满意,我建议您接受,方法是点击其分数下方的勾号,否则此问题看起来没有答案。
标签: haskell functional-programming