gpt4 book ai didi

ubuntu - 带有haskell的文件的字数

转载 作者:行者123 更新时间:2023-12-04 19:16:56 25 4
gpt4 key购买 nike

当我试图计算文件的字数时,我遇到了 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

关于ubuntu - 带有haskell的文件的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com