小编Hun*_*jos的帖子

覆盖Haskell中的+

我试图覆盖+符号,以学习如何定义自己的类型.我是Haskell的新手,我似乎无法克服这个错误.

这是我简单的新类型:

newtype Matrix x = Matrix x
(+):: (Num a, Num b, Num c) => Matrix [[a]] -> Matrix [[b]] -> Matrix [[c]]
x + y = Matrix zipWith (\ a b -> zipWith (+) a b) x y
Run Code Online (Sandbox Code Playgroud)

当我尝试将其加载到ghci时,我收到错误

linear_algebra.hs:9:42:
    Ambiguous occurrence ‘+’
    It could refer to either ‘Main.+’, defined at linear_algebra.hs:9:3
                          or ‘Prelude.+’,
                             imported from ‘Prelude’ at linear_algebra.hs:1:1
                             (and originally defined in ‘GHC.Num’)
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

用我的代码替换我的最后一行代码

x + y = Matrix zipWith (\ a b …
Run Code Online (Sandbox Code Playgroud)

constructor haskell types function matrix

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

标签 统计

constructor ×1

function ×1

haskell ×1

matrix ×1

types ×1