haskell 文件的字数统计
Word count of a file with haskell
当我尝试计算文件的字数时,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";
}
如有任何帮助,我们将不胜感激。谢谢!
您可以使用以下任一项:
main = readFile "/tmp/foo.txt" >>= print . length . words
或
main = do
contents <- readFile "/tmp/foo.txt"
print . length . words $ contents
当我尝试计算文件的字数时,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";
}
如有任何帮助,我们将不胜感激。谢谢!
您可以使用以下任一项:
main = readFile "/tmp/foo.txt" >>= print . length . words
或
main = do
contents <- readFile "/tmp/foo.txt"
print . length . words $ contents