2016-04-13 68 views
0

當試圖編譯我的代碼我得到:哈斯克爾:無法匹配,期望型「IO T0」與實際類型「整型」

[1 of 1] Compiling Main    (survey2.hs, survey2.o) 

survey2.hs:20:1: 
    Couldn't match expected type ‘IO t0’ with actual type ‘Integer’ 
    In the expression: main 
    When checking the type of the IO action ‘main’ 

我試着指定「9亂搞'作爲一組不同的類型輸入到主體,包括IO,IO t,IO t0,int等等。據我所知,基於我在其他地方的函數定義,如果Integer沒有輸入到函數中沒有其他功能可以正常工作。我不知道如何把正確的類型放入主要。

factorial:: Integer -> Integer 
factorial n 
    | n <= 1 = 1 
    | otherwise = n * factorial(n-1) 

binomial :: (Integer, Integer) -> Integer 
binomial (n, k) 
    | k > n  = 0 
    | k < 0  = 0 
    | otherwise = factorial(n)/(factorial(n-k) * factorial(k)) 

bell :: Integer -> Integer 
bell n 
    | n <= 1 = 1 
    | otherwise = sum [ binomial(n-1, k-1) * bell (k-1) | k<-[0..n-1] ] 

bellSum :: Integer -> Integer 
bellSum n = sum [ bell(k) | k<-[0..n] ] 

main = bell(9 :: Integer) 

回答

5

如果main是主模塊(通常稱爲Main)中,它必須具有類型IO a(通常IO())。

由於bell 9Integer類型不匹配。您需要打印Integerprint :: Show a => a -> IO ()Integer

main = print (bell 9) 

注意(/)沒有爲Integer工作,你需要使用div代替:

| otherwise = factorial(n) `div` (factorial(n-k) * factorial(k)) 
相關問題