![](/img/trans.png)
[英]Haskell powerset function - How to avoid Couldn't match expected type `IO t0' with actual type `[[Integer]]'
[英]Haskell: Couldn't match expected type 'IO t0' with actual type 'Integer'
當嘗試編譯我的代碼時,我得到:
[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’
我嘗試弄亂指定輸入到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 )
如果main
在主模塊中(通常稱為Main
),則它必須具有IO a
類型(通常是IO ()
)。
由於bell 9
類型為Integer
因此類型不匹配。 您需要使用以下命令打印Integer
print :: Show a => a -> IO ()
Integer
:
main = print (bell 9)
請注意, (/)
不適用於Integer
,您需要改用div
:
| otherwise = factorial(n) `div` (factorial(n-k) * factorial(k))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.