2015-11-16 181 views
-1

我使用下面的haskell代碼來捕捉錯誤,同時使用getLine在int中讀取。 CheckInt函數被調用時出現類型不匹配錯誤。請讓我知道我應該在哪裏修改類型。預期類型不匹配haskell

import System.Exit 
import Control.Exception 
funSeq n = do 
    if (n<4) then 1 else floor $ (fromIntegral (funSeq (n-1) + funSeq (n-2))) * ((fromIntegral.funSeq) (n-3)/(fromIntegral.funSeq) (n-4)) 


myFunc ('N':'T':'H':' ': restOfString) = do 
    checkInt restOfString 
    let num = (read restOfString :: Int) 
    if (num < 0) then showError else print (funSeq (num)) 

main = do 
putStrLn "Enter the code: " 
code <- getLine 
myFunc code 
main 


checkInt :: String -> IO() 
checkInt str = catch (seq (read str :: Int) $ return()) showError 

showError :: SomeException -> IO() 
showError _ = do 
    putStrLn("ERR") 
    exitSuccess 
+4

當問這樣的問題時,總是__指出錯誤__,指向該行。 – chi

回答

2

可能有其他錯誤,但下面跳出來對我說:

showError :: SomeException -> IO() 

myFunc ... = do 
    ... 
    if ... then showError else ... 

由於showError是一個函數,你可能應該在這裏給它一個說法。

相關問題