简体   繁体   中英

Haskell: Couldn't match expected type 'IO t0' with actual type 'Integer'

When trying to compile my code i'm getting:

[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’

I've tried messing around with specifying the '9' that is being input to main as a bunch of different types including IO, IO t, IO t0, int, etc. I understand that based on the function definitions I have in other places, that if an Integer is not input into the function that none of the other functions will work properly. I'm not sure How to put the proper type into the main.

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 )

If main is in the main module (usually called Main ), it must have the type IO a (usually IO () ).

Since bell 9 has type Integer the types don't match. You need to print the Integer with print :: Show a => a -> IO () the Integer :

main = print (bell 9)

Note that (/) does not work for Integer , you need to use div instead:

| otherwise = factorial(n) `div` (factorial(n-k) * factorial(k))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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