Function GHC韩元';我不运行这个函数,但编译它
代码如下:Function GHC韩元';我不运行这个函数,但编译它,function,haskell,functional-programming,Function,Haskell,Functional Programming,代码如下: finde_f x = if (x-2) mod 3 /= 0 then 1 else x - (x-2)/3 以下是运行时期间的错误: *Main> finde_f 6 <interactive>:170:1: No instance for (Fractional ((a10 -> a10 -> a10) -> a20 -> a0)) arising from a use of `finde
finde_f x =
if (x-2) mod 3 /= 0
then 1
else x - (x-2)/3
以下是运行时期间的错误:
*Main> finde_f 6
<interactive>:170:1:
No instance for (Fractional ((a10 -> a10 -> a10) -> a20 -> a0))
arising from a use of `finde_f'
Possible fix:
add an instance declaration for
(Fractional ((a10 -> a10 -> a10) -> a20 -> a0))
In the expression: finde_f 6
In an equation for `it': it = finde_f 6
<interactive>:170:9:
No instance for (Num ((a10 -> a10 -> a10) -> a20 -> a0))
arising from the literal `6'
Possible fix:
add an instance declaration for
(Num ((a10 -> a10 -> a10) -> a20 -> a0))
In the first argument of `finde_f', namely `6'
In the expression: finde_f 6
In an equation for `it': it = finde_f 6
完整代码,带更正:
-- Continuous Fraction -------------------------------------------------------------------
-- A --
cont_frac n d k =
if k == 1
then (n k) / (d k)
else (n k) / ((d k) + (cont_frac n d (k-1)))
-- B --
cont_frac_iter n d k count =
if count == k
then (n count) / (d count)
else (n count) / ((d count) + (cont_frac_iter n d k (count+1)))
-- e-2 Continuous Fraction ---------------------------------------------------------------
finde_cf k =
2 + (cont_frac_iter (\x -> 1) finde_f (k) (1))
-- Auxiliary Function --
finde_f x =
if mod (x-2) 3 /= 0
then 1
else fromIntegral x - (fromIntegral x-2)/3
mod
是一个前缀函数,但可以将其用作中缀
使用:
或
已更新
您尝试将积分
与分数
> :t (/)
(/) :: Fractional a => a -> a -> a
> :t mod
mod :: Integral a => a -> a -> a
所以,只需转换数字,如下所示:
> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
... else fromIntegral x - (fromIntegral x-2)/3
mod
是一个前缀函数,但可以将其用作中缀
使用:
或
已更新
您尝试将积分
与分数
> :t (/)
(/) :: Fractional a => a -> a -> a
> :t mod
mod :: Integral a => a -> a -> a
所以,只需转换数字,如下所示:
> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
... else fromIntegral x - (fromIntegral x-2)/3
谢谢这就解决了一个问题。在修复之后,我仍然得到一个错误。我已将其添加到帖子中。谢谢。就是这样。我会发布完整的代码,这样你就知道是怎么回事了。谢谢。这就解决了一个问题。在修复之后,我仍然得到一个错误。我已将其添加到帖子中。谢谢。就是这样。我将发布完整的代码,以便您了解其中的全部内容。只是澄清一下-这是一个编译时错误,而不是运行时错误。ghci一步完成编译和运行,我认为这引起了您的困惑-但这个错误肯定会在编译过程中出现。只是澄清一下-这是一个编译时错误,而不是运行时错误。ghci一步完成编译和运行,我想这会让您感到困惑,但在编译过程中肯定会出现这个错误。
> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
... else fromIntegral x - (fromIntegral x-2)/3