do块中的Haskell类型不匹配

do块中的Haskell类型不匹配,haskell,types,io,type-mismatch,Haskell,Types,Io,Type Mismatch,在学习Haskell“为了更大的利益”(即通过函数式语言的考试)时, 我遇到了一个奇怪的错误。我在每个do块(第一个块除外)中都会得到一个类型不匹配错误。更准确地说,编译器似乎希望得到一个列表 我认为这与IO操作有关 代码: --chaos.hs 导入系统.IO main::IO() main=do——无错误 inl字符串 extractLang file=do--error eof您完全正确,因为它与IO操作有关。首先,extractLang的正确类型是Handle->IO String。其次

在学习Haskell“为了更大的利益”(即通过函数式语言的考试)时, 我遇到了一个奇怪的错误。我在每个do块(第一个块除外)中都会得到一个类型不匹配错误。更准确地说,编译器似乎希望得到一个列表

我认为这与IO操作有关

代码:

--chaos.hs
导入系统.IO
main::IO()
main=do——无错误
inl字符串
extractLang file=do--error

eof您完全正确,因为它与IO操作有关。首先,
extractLang
的正确类型是
Handle->IO String
。其次,缺少两个
返回
s(原因相同):

extractLang::Handle->IO字符串
extractLang file=do

eof您完全正确,因为它与IO操作有关。首先,
extractLang
的正确类型是
Handle->IO String
。其次,缺少两个
返回
s(原因相同):

extractLang::Handle->IO字符串
extractLang file=do
eof
-- 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
}
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