haskell文件的字数

时间:2015-12-16 12:20:47

标签: ubuntu haskell functional-programming hugs

当我试图计算文件的单词时,我遇到了Haskell的问题。我只是一个初学者,这是我的第一个程序,所以我很确定这是一个非常简单的错误。 我正在使用拥抱来运行我的代码。直到现在我学会了如何从文件中读取,但我没有计算其中的单词。我的代码是这样的

main = do {
contents <- readFile "/tmp/foo.txt";
let contents2 = replace"."""contents;
let contents3 = replace"!"""contents2;
let lower = map toLower contents3;
let chop = words(lower);
let count = show(length chop)++"\n";
putStrln $"This file has"++count++"words";
}

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用以下任一项:

main = readFile "/tmp/foo.txt" >>= print . length . words

main = do
    contents <- readFile "/tmp/foo.txt"
    print . length . words $ contents