【问题标题】:How You Take Only The Last 10 Lines of a Text File in Haskell如何在 Haskell 中只获取文本文件的最后 10 行
【发布时间】:2021-10-14 00:07:11
【问题描述】:

在 Haskell(尾部)中仅获取文本文件的最后 10 行并输出它们的最佳方法是什么?

我正在尝试定义一个函数:

getLastTenLines :: String -> String
getLastTenLines s = unlines (take 10 (lines s))

但是,这需要文件的前 10 行。可以修改为取最后 10 行吗?

【问题讨论】:

标签: haskell


【解决方案1】:

你可以试试这个:

getLastLines :: Int -> String -> String
getLastLines n s = concat (reverse (take n (reverse (lines s))))

text = "Hello how are you? \n I'm fine how are you? \n Also good!"

tester = getLastLines 2 text
-- tester output: "I'm fine how are you? Also good!"

你给函数一个 n,你想要的最后一行的数量,以及一个文本所在的字符串,它会返回你想要的文本的最后 n 行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 2011-02-27
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    相关资源
    最近更新 更多