问题描述
其他问题和问题,虽然类似,不太像这一个。在这个特定的编译器错误中,Haskell GHC不会编译以下代码,原因如下。我完全不明白 - 代码很简单。
- factorial
fact :: int - > int
fact 0 = 1
fact n | n。 0 = n * fact(n-1)
main = print(fact 10)
$ b b
(错误:)
由于使用`>'$ $ b可能的修复:
将(ord int)添加到
的上下文中fact :: int - > int
在表达式中:n> 0
在
的模式保护的stmt中为fact的方程:
n> 0
在fact的方程中:fact n | n。你能解释一下这个问题吗? >
解决方案 Int
是你想要的:
fact :: int - > int
- >
fact :: Int - > Int
由于在Haskell中,类型需要以上限开头。
编辑:感谢Yuras对此进行评论:
或者如果你想要使用类型类:
fact :: Integral a => a - > a
您可以为类型变量命名,包括 int
。此外, Num
可能更适合您的目的,如果你想定义因子对一般数字。
other questions and problems, although similar, are not quite like this one. in this specific compiler error, the Haskell GHC won't compile the following code, for the following reason. I don't understand at all - the code is pretty straight forward.
--factorial
fact :: int -> int
fact 0 = 1
fact n | n > 0 = n * fact(n - 1)
main = print (fact 10)
(error:)
No instance for (Ord int) arising from a use of `>'
Possible fix:
add (Ord int) to the context of
the type signature for fact :: int -> int
In the expression: n > 0
In a stmt of a pattern guard for
an equation for `fact':
n > 0
In an equation for `fact': fact n | n > 0 = n * fact (n - 1)
Can you explain the problem to me?
解决方案 Int
is what you want:
fact :: int -> int
-->
fact :: Int -> Int
Since in Haskell, types need to begin with a cap.
Edit: Thank Yuras for commenting this:
Or if you want you could use a type class:
fact :: Integral a => a -> a
And you can name the type variable whichever you like, including int
. Also, Num
might fit your purpose better if you want to define factorial over general numbers.
这篇关于没有使用`>'(Haskell)产生的(Ord int)实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!