首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Haskell二次解查找器-误差

Haskell二次解查找器-误差
EN

Stack Overflow用户
提问于 2016-10-02 21:39:22
回答 3查看 630关注 0票数 2

我在大学里用的是Haskell,我要做的一个练习是做一个函数,给我一个二次方程的根,当我给它系数时,用以前的函数告诉我它有多少个解。以下是我所做的:

第一个函数,这个功能很好:

代码语言:javascript
运行
复制
nRoots :: Float -> Float -> Float -> Int
nRoots a b c | r<0 = 0
             | r==0 = 1
             | otherwise = 2
             where r = b^2-4*a*c

第二个功能,它不起作用:

代码语言:javascript
运行
复制
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 = []

以下是我遇到的错误:

代码语言:javascript
运行
复制
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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-10-02 22:11:33

您需要向nRoots,特别是roots a b c | nRoots a b c == 2 = ...提供适当的参数。

但是,您的错误消息告诉您,让我们看看如何从GHC读取此错误消息,以了解这是您的问题所在。我已经用几个区段标记了下面的错误消息。

代码语言:javascript
运行
复制
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--------------------

首先,让我们回顾一下==的类型。

代码语言:javascript
运行
复制
(==) :: 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 BSECTION C会告诉我们,在SECTION A中看到的错误只是一个更深层次的优势错误。

SECTION B实际上只是告诉您问题在SECTION A中的确切位置,所以这并不是什么新的东西。

SECTION C呢?还记得==的类型吗?结果是,==期望等式的两边都是相同类型的。现在,GHC看到了nRoots == 2,并期望nRoots2都是相同类型的。好的,Haskell中的数字文字被重载为Num a => a类型,这样它们可以同时表示Ints、Integers、Doubles、Rationals等。因此,现在GHC期望nRootsNum a => a类型,特别是a必须是Float -> Float -> Float -> Int,也就是说,它希望nRootsNum类型的实例。

正如GHC敏锐地暗示的那样,这实际上只是同一个问题的另一个症状!也就是说,您忘记实际向nRoots应用参数,因此GHC试图处理一个空函数,而不是函数的输出。

因此,Section ASection C都告诉您,您有相同的问题,即您试图使用一个函数本身--当您应该将参数完全应用到该函数并使用该函数的输出时。

票数 2
EN

Stack Overflow用户

发布于 2016-10-02 21:56:13

您不会向nRoots传递任何参数。您可以通过将系数传递给nRoots来修正错误。

代码语言:javascript
运行
复制
roots a b c | nRoots a b c ==2 = ...

错误消息告诉您,没有办法检查是否相等( Eq实例),比如nRoots,它有Float -> Float -> Float -> Int类型,也无法将数字2转换成具有相同类型的数字(没有Num实例)。这两个错误都是由表达式nRoots == 2引起的。

票数 5
EN

Stack Overflow用户

发布于 2016-10-02 21:59:24

您没有将函数nRoot应用于它在第二个函数中的守护参数中的参数。将函数nRoots应用于参数abc,然后您将能够比较它的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39821968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档