2011-12-20 59 views
1
countSequences :: Int -> Int -> Integer 

countSequences 0 m = 0 
countSequences m 0 = 0 
countSequences (n) (m) = if (n <= (m+1)) then (truncate((cee (m+1) (n) (0))) + truncate((countSequences (fromIntegral (n-1)) (fromIntegral (m))))) 
else truncate(countSequences (fromIntegral (n-1)) (fromIntegral (m))) 

factorial :: Float -> Float 
factorial 0 = 1 
factorial 1 = 1 
factorial x = x * factorial(x-1) 

cee :: Float -> Float -> Float -> Float 

cee x y z = if (x==y) then ((1)/(factorial ((x+z)-(y)))) else ((x) * (cee (x-1) (y) (z+1))) 

我真的不能明白爲什麼這個錯誤繼續來了..在截斷應該類型從浮點轉換爲整數所以..無法比擬的預期類型整數

回答

2

mInt類型(根據您的類型簽名countSequences:因此,所以是m + 1但是,你的函數cee需要一個Float,所以。類型檢查理直氣壯地抱怨

此外,你需要一對夫婦更多的修復,使這種類型的檢查下面是經過檢查的一個版本:。

countSequences :: Int -> Int -> Integer 
countSequences 0 m = 0 
countSequences m 0 = 0 
countSequences n m = 
    if n <= m + 1 
    then truncate $ 
     cee (fromIntegral (m+1)) (fromIntegral n) 0 + 
     fromIntegral (countSequences (n-1) m) 
    else countSequences (n-1) m 
+0

yeahh thanks dblhelix :) – 2011-12-25 00:26:02

5

的錯誤是:

Couldn't match expected type `Float' with actual type `Int' 
In the first argument of `(+)', namely `m' 
In the first argument of `cee', namely `(m + 1)' 
In the first argument of `truncate', namely 
    `((cee (m + 1) (n) (0)))' 

你看,問題是你傳遞一個Int的功能cee

在這裏,我清理你的代碼:

countSequences :: Int -> Int -> Integer 

countSequences 0 m = 0 
countSequences m 0 = 0 
countSequences n m = 
    if n <= m+1 
    then truncate (cee (fromIntegral (m+1)) (fromIntegral n) 0) + 
     countSequences (n-1) m 
    else countSequences (n-1) m 

factorial :: Float -> Float 
factorial 0 = 1 
factorial 1 = 1 
factorial x = x * factorial (x-1) 

cee :: Float -> Float -> Float -> Float 

cee x y z = 
    if (x==y) 
    then 1/factorial (x+z-y) 
    else x * cee (x-1) y (z+1) 
+0

'fromIntegral m + 1'應該可能是'fromIntegral m + 1'或'fromIngral(m + 1)',因爲沒有意識到應用程序綁定的是最常見的錯誤:) – ehird 2011-12-20 22:04:51

+0

@ehird:再右吧。我很懶,只是使用'(fromIntegral x)+ 1 == fromInteral(x + 1)'這個便利的事實。我會編輯答案。 – opqdonut 2011-12-20 22:06:30

+2

從技術上講,對於足夠病態的'x',你可以得到溢出預轉換,這可能會導致不同的結果,因爲Float不會以相同的方式溢出......但是,足夠接近所有合理的值:) – ehird 2011-12-20 22:07:41

相關問題