小编hdi*_*zle的帖子

为什么这个Functor实例不正确?

我写了这段代码:

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类型设置仿函数,然后遍历列表?

haskell functional-programming

3
推荐指数
1
解决办法
139
查看次数

没有使用'+'Haskell引起的(Num a)实例

我无法弄清楚为什么这不起作用:

final' :: [a] -> a
final' lst = foldl(\accum x -> accum - accum + x) 0 lst
Run Code Online (Sandbox Code Playgroud)

我总是得到错误没有为使用'+'而产生的(Num a)实例

haskell

1
推荐指数
1
解决办法
537
查看次数

标签 统计

haskell ×2

functional-programming ×1