Haskell readFile:无法将预期类型“[String]”与实际类型“IO String”相匹配

Haskell readFile: Couldn't match expected type ‘[String]’ with actual type ‘IO String’

我正在尝试将文件读入函数以计算文件中字符的出现频率。所以我正在尝试以下操作:

charCount :: String -> [(Char, Int)]
charCount input = M.toList $ M.fromListWith (+) [(c, 1) | c <- input]

calculate :: FilePath -> [(Char, Int)]
calculate fp = do
  c <- readFile fp
  charCount c

但我收到以下错误:

FileWriter.hs:13:8: Couldn't match expected type ‘[String]’ …
                with actual type ‘IO String’
    In a stmt of a 'do' block: c <- readFile fp
    In the expression:
      do { c <- readFile fp;
           charCount c }
Compilation failed.

由于 calculate 调用 readFile 函数,该函数 return 是一个包含在 IO monad 中的值,因此函数 calculate 必须 return 一个IO 值也是如此,调用 charCount 的结果(纯计算)必须 returned 以便将 [(Char, Int)] 包装到 monad 中。

下一个示例适用于 ghc 7.10.1

import qualified Data.Map as M

charCount :: String -> [(Char, Int)]
charCount input = M.toList $ M.fromListWith (+) [(c, 1) | c <- input]

calculate :: FilePath -> IO [(Char, Int)]
calculate fp =
    readFile fp >>= \c ->
    return (charCount c)