vik*_*ata 0 parsing haskell overloading operator-keyword
我有一个hs文件,试图重载&&运算符
(&&)::Bool->Bool->Bool
True && x = x
False && _ = False
and' :: (Bool)->Bool
and' xs=foldr (&&) True xs
Run Code Online (Sandbox Code Playgroud)
在Prelude中导入时,出现错误:
Ambiguous occurrence ‘&&’
It could refer to either ‘Main.&&’, defined at D:\baby.hs:2:6
or ‘Prelude.&&’,
imported from ‘Prelude’ at D:\baby.hs:1:1
(and originally defined in ‘GHC.Classes’)
Run Code Online (Sandbox Code Playgroud)
所以我改变了最后一行
and' xs=foldr (Main.&&) True xs
Run Code Online (Sandbox Code Playgroud)
现在它打印新的错误消息:
Couldn't match expected type ‘t0 Bool’ with actual type ‘Bool’
In the third argument of ‘foldr’, namely ‘xs’
In the expression: foldr (Main.&&) True xs
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?谢谢.
正如@zakyggaps在他的评论中所说,(Bool)
是一样的Bool
.你明确的意思[Bool]
.此外,您并没有真正"重载"此功能,而是在不同模块中定义类似命名的功能.充满"阴影",但实际上并非如此.
归档时间: |
|
查看次数: |
73 次 |
最近记录: |