首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GHC不会运行这个函数,但是会编译它。

GHC不会运行这个函数,但是会编译它。
EN

Stack Overflow用户
提问于 2014-01-09 23:17:32
回答 1查看 503关注 0票数 1

这是代码:

代码语言:javascript
复制
finde_f x =
    if (x-2) mod 3 /= 0
    then 1
    else x - (x-2)/3

以下是运行时的错误:

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

我不知道这里发生了什么。我希望您能帮助我理解为什么这个(非常)简单的函数不能运行。是因为mod还是/?我怎么才能解决这个问题?

编辑:更改为mod之后

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

全码,更正如下:

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-09 23:25:00

mod是一个前缀函数,但您可以使用它作为infix。

使用:

代码语言:javascript
复制
mod (x-2) 3 /= 0    --prefix

代码语言:javascript
复制
(x-2) `mod` 3 /= 0  --infix

更新

您尝试将IntegralFractional结合使用

代码语言:javascript
复制
> :t (/)
(/) :: Fractional a => a -> a -> a

> :t mod
mod :: Integral a => a -> a -> a

所以,只要转换数字,就像这样:

代码语言:javascript
复制
> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b

... else fromIntegral x - (fromIntegral x-2)/3
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21033225

复制
相关文章

相似问题

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