Haskell 编译错误-无法推断Ord 浮动a=>a->a->a->a nRaizes a b c=let r=b^2-4*a*c 如果ra->a->a”而产生的(Ord a) 在Ficha1.hs:29:14-43 可能的解决方案: 将(Ord a)添加到 nRaizes::Floating a=>a->a->a->a的类型签名 在表达式中:ra->a 在Ficha1.hs:29:14-43 可能的解决方案: 将(等式a)添加到 nRaizes::Floating a=>a->a->a->a的类型签名 在表达式中:r==0 在表达式中:如果r==0,则为1,否则为2 在表达式中:如果r
所以我得到了这个代码,在这里我检查方程的根数。但是当我试图编译它时,它给了我这个Haskell 编译错误-无法推断Ord 浮动a=>a->a->a->a nRaizes a b c=let r=b^2-4*a*c 如果ra->a->a”而产生的(Ord a) 在Ficha1.hs:29:14-43 可能的解决方案: 将(Ord a)添加到 nRaizes::Floating a=>a->a->a->a的类型签名 在表达式中:ra->a 在Ficha1.hs:29:14-43 可能的解决方案: 将(等式a)添加到 nRaizes::Floating a=>a->a->a->a的类型签名 在表达式中:r==0 在表达式中:如果r==0,则为1,否则为2 在表达式中:如果r,haskell,types,Haskell,Types,所以我得到了这个代码,在这里我检查方程的根数。但是当我试图编译它时,它给了我这个 nRaizes :: Floating a => a -> a -> a -> a nRaizes a b c = let r = b^2 - 4 * a * c in if r < 0 then 0 else if r == 0
nRaizes :: Floating a => a -> a -> a -> a
nRaizes a b c = let r = b^2 - 4 * a * c
in if r < 0
then 0
else if r == 0
then 1
else 2
无法推断由于使用“a->a->a->a”而产生的(Ord a)
在Ficha1.hs:29:14-43
可能的解决方案:
将(Ord a)添加到
nRaizes::Floating a=>a->a->a->a的类型签名
在表达式中:r<0
在表达式中:如果r<0,则0 else如果r==0,则1 else 2
在表达式中:
设r=b^2-4*a*c
如果r<0,则为0,如果r==0,则为1,否则为2
Ficha1.hs:33:32:
无法推断因使用“=”而产生的(等式a)
从上下文(浮动a)
由的类型签名绑定
nRaizes::浮动a=>a->a->a->a
在Ficha1.hs:29:14-43
可能的解决方案:
将(等式a)添加到
nRaizes::Floating a=>a->a->a->a的类型签名
在表达式中:r==0
在表达式中:如果r==0,则为1,否则为2
在表达式中:如果r<0,则0 else如果r==0,则1 else 2
是的,我是个乞丐。我真的无法理解到底出了什么问题。这是个浮点数,为什么不能比较呢?
我只知道它和签名有关,因为如果我把它去掉,它就会工作
这是个浮点数,为什么不能比较呢
不,它本身不是一个浮点数<代码>浮动严格来说意味着可以在其上执行三角函数和双曲函数。这是否意味着它是一个浮点数,是一个不同的方面(尽管它有点相关)
尽管如此,这并不意味着你可以比较两个元素。但是,这不是问题:我们可以向其添加typeclassOrd a
:
Could not deduce (Ord a) arising from a use of ‘<’
from the context (Floating a)
bound by the type signature for
nRaizes :: Floating a => a -> a -> a -> a
at Ficha1.hs:29:14-43
Possible fix:
add (Ord a) to the context of
the type signature for nRaizes :: Floating a => a -> a -> a -> a
In the expression: r < 0
In the expression: if r < 0 then 0 else if r == 0 then 1 else 2
In the expression:
let r = b ^ 2 - 4 * a * c
in if r < 0 then 0 else if r == 0 then 1 else 2
Ficha1.hs:33:32:
Could not deduce (Eq a) arising from a use of ‘==’
from the context (Floating a)
bound by the type signature for
nRaizes :: Floating a => a -> a -> a -> a
at Ficha1.hs:29:14-43
Possible fix:
add (Eq a) to the context of
the type signature for nRaizes :: Floating a => a -> a -> a -> a
In the expression: r == 0
In the expression: if r == 0 then 1 else 2
In the expression: if r < 0 then 0 else if r == 0 then 1 else 2
此外,尽管大多数人喜欢圣诞节。大多数程序员不喜欢圣诞树作为函数定义。也许更优雅的方法是使用防护装置,而不是如果
-那么
-否则
s:
nRaizes :: (Num a, Ord a) => a -> a -> a -> a
nRaizes a b c = let r = b^2 - 4 * a * c
in if r < 0
then 0
else
if r == 0
then 1
else 2
nRaizes :: (Num a, Ord a) => a -> a -> a -> a
nRaizes a b c = let r = b^2 - 4 * a * c
in if r < 0
then 0
else
if r == 0
then 1
else 2
nRaizes :: (Num a, Ord a) => a -> a -> a -> a
nRaizes a b c | r < 0 = 0
| r == 0 = 1
| otherwise = 2
where r = b*b - 4*a*c
nRaizes :: (Num a, Ord a, Num b) => a -> a -> a -> b
nRaizes a b c | r < 0 = 0
| r == 0 = 1
| otherwise = 2
where r = b*b - 4*a*c