【发布时间】:2019-10-07 11:17:49
【问题描述】:
我在大学里使用 Haskell,我必须做的一个练习是制作一个函数,当我给它系数时,它给我一个二次方程的根,使用前面的函数告诉我它有多少个解.这是我所做的:
第一个函数,这个很好用:
nRoots :: Float -> Float -> Float -> Int
nRoots a b c | r<0 = 0
| r==0 = 1
| otherwise = 2
where r = b^2-4*a*c
第二个功能,不起作用:
roots :: Float -> Float -> Float -> [Float]
roots a b c | nRoots==2 = [(-b-sqrt(b^2-4*a*c))/(2*a),(-b+sqrt(b^2-4*a*c))/(2*a)]
| nRoots==1 = [-b/(2*a)]
| otherwise = []
这是我得到的错误:
raizes.hs:8:21:
No instance for (Eq (Float -> Float -> Float -> Int))
(maybe you haven't applied enough arguments to a function?)
arising from a use of ‘==’
In the expression: nRoots == 2
In a stmt of a pattern guard for
an equation for ‘roots’:
nRoots == 2
In an equation for ‘roots’:
roots a b c
| nRoots == 2
= [(- b - sqrt (b ^ 2 - 4 * a * c)) / (2 * a),
(- b + sqrt (b ^ 2 - 4 * a * c)) / (2 * a)]
| nRoots == 1 = [- b / (2 * a)]
| otherwise = []
raizes.hs:8:23:
No instance for (Num (Float -> Float -> Float -> Int))
(maybe you haven't applied enough arguments to a function?)
arising from the literal ‘2’
In the second argument of ‘(==)’, namely ‘2’
In the expression: nRoots == 2
In a stmt of a pattern guard for
an equation for ‘roots’:
nRoots == 2
知道发生了什么吗??
提前致谢
编辑:感谢所有答案!我现在因为没有注意到它而感到很愚蠢:X
【问题讨论】: