我写了这段代码:
newtype Pixel a = Pixel (a,a,a) deriving (Show)
instance Functor [Pixel Int] where
fmap f [] = []
fmap f [Pixel(a,b,c)] = [Pixel(f a, b, c)]
Run Code Online (Sandbox Code Playgroud)
我希望仿函数应用于Pixel类型中的第一个元素,但我不断收到此错误:
New.hs:17:18: error:
• Expecting one fewer arguments to ‘[Pixel Int]’
Expected kind ‘* -> *’, but ‘[Pixel Int]’ has kind ‘*’
• In the first argument of ‘Functor’, namely ‘[Pixel Int]’
In the instance declaration for ‘Functor [Pixel Int]’
Run Code Online (Sandbox Code Playgroud)
我在这个问题上很丢失,有没有办法在整个列表中应用仿函数?或者我是否需要为单个Pixel类型设置仿函数,然后遍历列表?
我无法弄清楚为什么这不起作用:
final' :: [a] -> a
final' lst = foldl(\accum x -> accum - accum + x) 0 lst
Run Code Online (Sandbox Code Playgroud)
我总是得到错误没有为使用'+'而产生的(Num a)实例