2016-04-26 22 views
0

Haskellを学んでいる間に、より良い結果を得るために、 私は奇妙なエラーに遭遇しました。すべてのdo-blockでType不一致エラーが発生します(最初のものを除く)。より正確にするために、コンパイラは何かのリストを期待しているようです。私はそれがIO操作とは何かを持っていると思うdoブロックのHaskellタイプの不一致

...

コード:

-- chaos.hs 
import System.IO 

main :: IO() 
main = do           -- no error 
     inl <- openFile "dictionary.txt" ReadMode 
     let lang = extractLang (inl) 
     hClose inl 

extractLang :: Handle -> String 
extractLang file = do        --error 
        eof <- hIsEOF file 
        if eof 
         then do hClose file   --error 
           "none" 
         else do line <- hGetLine file --error 
           if length (words line) == 1 
           then line 
           else extractLang file 

エラーログ:

chaos.hs:12:27: 
Couldn't match type ‘IO’ with ‘[]’ 
Expected type: [Bool] 
    Actual type: IO Bool 
In a stmt of a 'do' block: eof <- hIsEOF file 
In the expression: 
    do { eof <- hIsEOF file; 
     if eof then 
      do { hClose file; 
       .... } 
     else 
      do { line <- hGetLine file; 
       .... } } 

chaos.hs:14:31: 
    Couldn't match type ‘IO’ with ‘[]’ 
    Expected type: [()] 
     Actual type: IO() 
    In a stmt of a 'do' block: hClose file 
    In the expression: 
     do { hClose file; 
      "none" } 

chaos.hs:16:39: 
    Couldn't match type ‘IO’ with ‘[]’ 
    Expected type: [String] 
     Actual type: IO String 
     In a stmt of a 'do' block: line <- hGetLine file 
     In the expression: 
     do { line <- hGetLine file; 
      if length (words line) == 1 then line else extractLang file 
} 

答えて

4

あなたは完全にそれを持っている右のことですIO操作と関係がある。まず、extractLangの適切なタイプはHandle -> IO Stringです。次に、同じ理由でreturnが欠落しています。

extractLang :: Handle -> IO String 
extractLang file = do         
        eof <- hIsEOF file 
        if eof 
         then do hClose file    
           return "none"   -- return 
         else do line <- hGetLine file 
           if length (words line) == 1 
           then return line  -- return 
           else extractLang file 
関連する問題