2014-01-09 13 views
1

這是代碼: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還是/?我怎樣才能解決這個問題?


編輯:

*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 

全碼,與修正:更改爲mod

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

只是爲了澄清 - 這是一個編譯時錯誤,而不是運行時錯誤。 ghci編譯和運行一步到位,我認爲這會引起你的困惑 - 但編譯過程中肯定會出現該錯誤。 – Carl

回答

4

mod是一個前綴功能,但是你把它作爲中綴。

用途:

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

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

修訂

您嘗試使用IntegralFractional

> :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 
+0

謝謝。這修復了一部分。修復後,仍然出現錯誤。我已將它添加到帖子中。 –

+0

@Fiire我更新了我的答案 – wit

+0

謝謝。這使得它。我將發佈完整的代碼,以便了解關於什麼。 –