错误:类出现“Ord”不明确
data (Ord a) => Stree a = Null
| Fork (Stree a) a (Stree a)
mkStree :: (Ord a) => [a] -> Stree a
mkStree [] = Null
mkStree (x:xs) = Fork (mkStree smaller) x (mkStree larger)
where (smaller,larger) = partition (<= x) xs
partition :: (a->Bool) -> [a] -> ([a],[a])
partition p xs = ([ x | x <- xs, p x],
[ y | y <- xs, (not . p) y])
我该如何解决这个故障 -->不明确的类出现“Ord” * 可以参考:Hugs.Prelude.Ord
data (Ord a) => Stree a = Null
| Fork (Stree a) a (Stree a)
mkStree :: (Ord a) => [a] -> Stree a
mkStree [] = Null
mkStree (x:xs) = Fork (mkStree smaller) x (mkStree larger)
where (smaller,larger) = partition (<= x) xs
partition :: (a->Bool) -> [a] -> ([a],[a])
partition p xs = ([ x | x <- xs, p x],
[ y | y <- xs, (not . p) y])
how can i fix this failure --> Ambiguous class occurrence "Ord"
* Could refer to: Hugs.Prelude.Ord
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法重现此错误,但我可以进行一些猜测。 “不明确的类出现”错误意味着作用域中存在多个“Ord”定义,因此真正的问题不在您在此处包含的代码中。问题是您导入的模块之一由于某种原因重新定义了“Ord”,或者您在代码中重新定义了它。无论哪种方式,唯一可行的方法是隐藏或限定其他定义(例如 Prelude 中的默认定义)。如果您尝试使用非标准 Ord 实现,则需要导入隐藏 Ord 的 Prelude:
如果您不是故意使用非标准 Ord,那么您需要弄清楚第二个 Ord 的位置来自并删除或隐藏它。不过,您的问题中没有足够的信息让我能够说出如何做到这一点。我希望错误消息也比您在此处列出的内容长,因为它应该显示 Ord 的两个定义的位置。
I cannot reproduce this error, but I can take some guesses. The "Ambiguous class occurrence" error means that there is more than one definition of "Ord" in scope, so the real problem is not in the code you've included here. The problem is either that one of the modules you're importing redefines "Ord" for some reason, or you're redefining it in your code. Either way, the only way that can work is if other definitions (such as the default one in the Prelude) are hidden or qualified. If you're trying to use a non-standard Ord implementation, you'll need to import the Prelude hiding Ord:
If you're not intentionally using a non-standard Ord, then you'll need to figure out where the second one is coming from and remove or hide it. There's not enough information in your question for me to be able to say how to do so, though. I would expect the error message is also longer than what you've listed here, as it should show the location of both definitions of Ord.
如果从数据声明中删除 Ord 上下文会发生什么?
数据声明上的类上下文非常不直观,我怀疑这可能会导致 Hugs 出错。
无论如何,您都不应该将上下文放在数据或新类型声明上。它们对于某些 GHC 扩展很有用,但我认为 Hugs 不支持任何这些情况。
What happens if you remove the Ord context from the data declaration?
Class contexts on data declarations are pretty non-intuitive, and I suspect that may be causing Hugs to error.
In any case, you shouldn't put contexts on data or newtype declarations. They're useful with certain GHC extensions, but I don't think Hugs supports any of those cases.