繁体   English   中英

Haskell:无法将期望的类型“ IO t0”与实际类型“ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM