GHC не запускает эту функцию, но компилирует ее.

Это код:

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

Я не уверен, что здесь происходит. Надеюсь, вы поможете мне понять, почему эта (очень) простая функция не работает. Это из-за 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

Полный код с исправлением:

-- 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

person OFRBG    schedule 09.01.2014    source источник
comment
Просто чтобы уточнить - это ошибка времени компиляции, а не ошибка времени выполнения. ghci компилирует и запускает вещи за один шаг, что, я думаю, вызвало у вас замешательство, но эта ошибка определенно проявляется во время компиляции.   -  person Carl    schedule 10.01.2014


Ответы (1)


mod — это префиксная функция, но вы используете ее как инфиксную.

Использовать:

mod (x-2) 3 /= 0    --prefix

or

(x-2) `mod` 3 /= 0  --infix

ОБНОВЛЕНО

Вы пытаетесь использовать Integral с Fractional

> :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
person wit    schedule 09.01.2014
comment
Спасибо. Это исправляет часть. После исправления все равно получаю ошибку. Я добавил его в пост. - person OFRBG; 10.01.2014
comment
Спасибо. Это делает это. Я опубликую полный код, чтобы вы видели, что это такое. - person OFRBG; 10.01.2014