Haskell二次解查找器-错误
我在大学里用的是Haskell,我要做的一个练习是做一个函数,当我给一个二次方程的系数时,它会给我一个二次方程的根,使用一个先前的函数告诉我它有多少解。以下是我所做的: 第一个功能,这个功能很好:Haskell二次解查找器-错误,haskell,quadratic,Haskell,Quadratic,我在大学里用的是Haskell,我要做的一个练习是做一个函数,当我给一个二次方程的系数时,它会给我一个二次方程的根,使用一个先前的函数告诉我它有多少解。以下是我所做的: 第一个功能,这个功能很好: nRoots :: Float -> Float -> Float -> Int nRoots a b c | r<0 = 0 | r==0 = 1 | otherwise = 2 where r =
nRoots :: Float -> Float -> Float -> Int
nRoots a b c | r<0 = 0
| r==0 = 1
| otherwise = 2
where r = b^2-4*a*c
下面是我得到的错误:
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你没有把任何参数传递给
nRoots
。您可以通过将系数传递到nRoots
roots a b c | nRoots a b c ==2 = ...
错误消息告诉您,对于类型为
Float->Float->Float->Int
的nRoots
,无法检查是否相等(一个Eq
实例),并且无法将数字2
转换为具有相同类型的数字(无Num
实例)。这两个错误都是由表达式nRoots==2
引起的。您没有将函数nRoot
应用于第二个函数中的参数。将函数nRoots
应用于参数a
、b
和c
,然后您将能够比较其结果
您需要为
nRoots
提供适当的参数,特别是根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
部分A
告诉您,=
需要相等类型类(Eq
)的实例才能工作,因为对于任何具有Eq
实例的类型,它都会过载。不幸的是,nRoots
的Eq
没有(也不可能是不重要的)实例,因为nRoots
本身就是一个函数(想想停止问题)。GHC在这里给出的提示正是您的问题所在,即GHC注意到您正在尝试比较函数的相等性(在本例中是一个数字),并建议可能您没有对函数应用足够的参数?
好的,就在第A节之后,我们似乎已经知道了您面临的问题,但是我们不要太匆忙,也许是第B节和第C节将表明您在第A节中看到的错误只是由更深层次的错误引起的高级错误
嗯,第B节实际上只是告诉你问题在第A节的确切位置,所以这不是什么新鲜事
那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试图处理一个裸函数,而不是函数的输出
所以A节
和C节
都告诉你,你有同样的问题,即你试图使用一个函数本身nRoots
,而你应该将你的参数完全应用到该函数,并使用该函数的输出
(==) :: Eq a => a -> a -> Bool