Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell:无法将预期类型“IO t0”与实际类型“Integer”匹配_Haskell_Functional Programming_Type Mismatch - Fatal编程技术网

Haskell:无法将预期类型“IO t0”与实际类型“Integer”匹配

Haskell:无法将预期类型“IO t0”与实际类型“Integer”匹配,haskell,functional-programming,type-mismatch,Haskell,Functional Programming,Type Mismatch,当试图编译我的代码时,我得到: [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’ 我尝试过将输

当试图编译我的代码时,我得到:

[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等。我知道,根据我在其他地方的函数定义,如果没有将整数输入到函数中,那么其他函数都无法正常工作。我不知道如何把正确的类型放入主

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的IO类型

由于bell 9具有Integer类型,因此类型不匹配。您需要使用print::Showa=>a->IO打印整数:

请注意/不适用于Integer,您需要改用div:

如果main位于通常称为main的主模块中,则它必须具有通常为IO的IO类型

由于bell 9具有Integer类型,因此类型不匹配。您需要使用print::Showa=>a->IO打印整数:

请注意/不适用于Integer,您需要改用div:

main = print (bell 9)
| otherwise = factorial(n) `div` (factorial(n-k) * factorial(k))