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

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函数,该函数返回包裹在IO monad中的值,因此calculate函数也必须返回IO值,并且对charCount的调用结果(纯计算)也必须是return ed,才能将[(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)

09-30 21:10