[英]GHC won't run this function, but does compile it
This is the code: 这是代码:
finde_f x =
if (x-2) mod 3 /= 0
then 1
else x - (x-2)/3
These are the errors during run-time: 这些是运行时错误:
*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
I'm not sure what is happening here. 我不确定这里发生了什么。 I hope you can help me understand why this (very) simple function doesn't run.
希望您能帮助我理解为什么这个(非常)简单的功能无法运行。 Is it because of
mod
or /
? 是因为
mod
或/
吗? How can I fix this? 我怎样才能解决这个问题?
Edit: After changing to mod
: 编辑:更改为
mod
:
*Main> finde_f 3
<interactive>:12:1:
No instance for (Integral a0) arising from a use of `finde_f'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Integral Int -- Defined in `GHC.Real'
instance Integral Integer -- Defined in `GHC.Real'
instance Integral GHC.Types.Word -- Defined in `GHC.Real'
In the expression: finde_f 3
In an equation for `it': it = finde_f 3
<interactive>:12:9:
No instance for (Num a0) arising from the literal `3'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus three others
In the first argument of `finde_f', namely `3'
In the expression: finde_f 3
In an equation for `it': it = finde_f 3
Full-code, with correction: 完整代码,带有更正:
-- 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
is a prefix function, but you use it as infix. mod
是一个前缀函数,但是您可以将其用作中缀。
Use: 采用:
mod (x-2) 3 /= 0 --prefix
or 要么
(x-2) `mod` 3 /= 0 --infix
UPDATED 更新
You try to use Integral
with Fractional
您尝试将
Integral
与Fractional
结合使用
> :t (/)
(/) :: Fractional a => a -> a -> a
> :t mod
mod :: Integral a => a -> a -> a
So, just convert numerals, like this: 因此,只需转换数字,如下所示:
> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
... else fromIntegral x - (fromIntegral x-2)/3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.