(Show(Double - > Double))没有实例 [英] No instance for (Show (Double -> Double))

查看:102
本文介绍了(Show(Double - > Double))没有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,它使用牛顿的方法来近似某个数字的平方根。问题是,当我运行它时,我得到一个错误.. 什么是错的,我该如何解决它?

  newtonRootSequence :: Double  - > [Double] 
newtonRootSequence d = newtonSequenceGenerator d 1

newtonSequenceGenerator :: Double - >双 - > [Double]
newtonSequenceGenerator d xn = nxplus1:newtonSequenceGenerator d nxplus1
其中nxplus1 =(xn + d / xn)/ 2

newtonRoot :: Double - >双 - > (xs !!指数-1),(xs !!指数) - (xs !!指数-1)(xs !!指数-1) =ε
其中xs = newtonRootSequence d

错误:

 < interactive>:2:1:error:
*没有实例用于(Show(Double - > Double))
使用`print'
引起的
(也许你没有给函数加足够的参数?)
*在一个交互式GHCi命令中:print it

运行它应该像下面这样:


$ newtonRoot 35



解决方案

在Haskell中,所有函数都是curryfied的,

  newtonRoot :: Double  - > Double  - > Double 

是隐藏的括号:

  newtonRoot :: Double - >(Double  - > Do如果你提供了一个参数 newtonRoot 35   




>你有

 (newtonRoot 35):: Double  - > Double 

和函数 f :: Double - > Double 不是Show类的实例



您需要最后为您的函数值提供最后一个参数:

 (newtonRoot 35 2):: Double 

Double可以显示

I have the following code which uses Newton's method to approximate the square root of some number. The problems is that when I run it, I get an error..What is wrong and how can I fix it?

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

Running it should be like the following:

$newtonRoot 35

解决方案

In Haskell all function are curryfied, so, your function

newtonRoot:: Double -> Double -> Double

the are "hidden parenthesis":

newtonRoot:: Double -> (Double -> Double)

if you provide one argument newtonRoot 35 you have

(newtonRoot 35) :: Double -> Double

and a function f :: Double -> Double is not instance of Show type class

You need to finally provide the last argument to your function value:

(newtonRoot 35 2) :: Double

Double can me showed

这篇关于(Show(Double - > Double))没有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆