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

Bri*_*ore 0 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’
Run Code Online (Sandbox Code Playgroud)

我已经尝试过将指定'9'输入到main作为一堆不同的类型,包括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 )
Run Code Online (Sandbox Code Playgroud)

Zet*_*eta 5

如果main在主模块中(通常称为Main),则必须具有类型IO a(通常IO ()).

由于bell 9有类型Integer的类型不匹配.您需要打印Integerprint :: Show a => a -> IO ()Integer:

main = print (bell 9)
Run Code Online (Sandbox Code Playgroud)

请注意,这(/)不起作用Integer,您需要使用div:

| otherwise = factorial(n) `div` (factorial(n-k) * factorial(k))
Run Code Online (Sandbox Code Playgroud)