我在大学里用的是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
发布于 2016-10-02 22:11:33
您需要向nRoots
,特别是roots a b c | nRoots a b c == 2 = ...
提供适当的参数。
但是,您的错误消息告诉您,让我们看看如何从GHC读取此错误消息,以了解这是您的问题所在。我已经用几个区段标记了下面的错误消息。
raizes.hs:8:21:
---------------------BEGIN SECTION A--------------------
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
---------------------END SECTION A--------------------
---------------------BEGIN SECTION B--------------------
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 = []
---------------------END SECTION B--------------------
raizes.hs:8:23:
---------------------BEGIN SECTION C--------------------
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
---------------------END SECTION C--------------------
首先,让我们回顾一下==
的类型。
(==) :: Eq a => a -> a -> Bool
SECTION A
告诉您,==
需要相等类型类(Eq
)的实例才能工作,因为对于任何具有Eq
实例的类型,它都是重载的。不幸的是,由于Eq
本身是一个函数(请考虑停止问题),所以对于nRoots
来说,nRoots
没有(也不可能是非常重要的)实例。GHC在这里给出的提示正是您的问题,即GHC注意到您试图比较函数的相等性(在本例中为数字),并建议maybe you haven't applied enough arguments to a function?
好的,就在SECTION A
之后,我们似乎已经知道了你面临的问题,但是我们不要太仓促,也许是SECTION B
和SECTION C
会告诉我们,在SECTION A
中看到的错误只是一个更深层次的优势错误。
SECTION B
实际上只是告诉您问题在SECTION A
中的确切位置,所以这并不是什么新的东西。
那SECTION C
呢?还记得==
的类型吗?结果是,==
期望等式的两边都是相同类型的。现在,GHC看到了nRoots == 2
,并期望nRoots
和2
都是相同类型的。好的,Haskell中的数字文字被重载为Num a => a
类型,这样它们可以同时表示Int
s、Integer
s、Double
s、Rational
s等。因此,现在GHC期望nRoots
为Num a => a
类型,特别是a
必须是Float -> Float -> Float -> Int
,也就是说,它希望nRoots
是Num
类型的实例。
正如GHC敏锐地暗示的那样,这实际上只是同一个问题的另一个症状!也就是说,您忘记实际向nRoots
应用参数,因此GHC试图处理一个空函数,而不是函数的输出。
因此,Section A
和Section C
都告诉您,您有相同的问题,即您试图使用一个函数本身--当您应该将参数完全应用到该函数并使用该函数的输出时。
发布于 2016-10-02 21:56:13
您不会向nRoots
传递任何参数。您可以通过将系数传递给nRoots
来修正错误。
roots a b c | nRoots a b c ==2 = ...
错误消息告诉您,没有办法检查是否相等( Eq
实例),比如nRoots
,它有Float -> Float -> Float -> Int
类型,也无法将数字2
转换成具有相同类型的数字(没有Num
实例)。这两个错误都是由表达式nRoots == 2
引起的。
发布于 2016-10-02 21:59:24
您没有将函数nRoot
应用于它在第二个函数中的守护参数中的参数。将函数nRoots
应用于参数a
、b
和c
,然后您将能够比较它的结果。
https://stackoverflow.com/questions/39821968
复制相似问题