我有以下代码,它使用牛顿法来近似某个数字的平方根。问题是当我运行它时,我收到错误..出了什么问题,我该如何解决?
newtonRootSequence :: Double -> [Double]
newtonRootSequence d = newtonSequenceGenerator d 1
newtonSequenceGenerator :: Double -> Double -> [Double]
newtonSequenceGenerator d xn = nxplus1 : newtonSequenceGenerator d nxplus1
where nxplus1 = (xn + d / xn) / 2
newtonRoot:: Double -> Double -> Double
newtonRoot d epsilon = head ([xs !! index | index <- [1..((length xs) - 1)], (xs !! index) - (xs !! index - 1) <= epsilon]
where xs = newtonRootSequence d
Error:
<interactive>:2:1: error:
* No instance for (Show (Double -> Double))
arising from a use of `print'
(maybe you haven't applied a function to enough arguments?)
* In a stmt of an interactive GHCi command: print it
运行它应该如下所示:
$牛顿根 35
在 Haskell 中,所有函数都被柯里化,所以,你的函数
newtonRoot:: Double -> Double -> Double
是“隐藏括号”:
newtonRoot:: Double -> (Double -> Double)
如果你提供一个参数newtonRoot 35
你有
(newtonRoot 35) :: Double -> Double
和一个函数f :: Double -> Double
不是 Show 类型类的实例
您最终需要为函数值提供最后一个参数:
(newtonRoot 35 2) :: Double
我可以显示双倍吗
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)