list - F# - 创建程序来读取文件并反向打印,如何删除所有新行引号?

标签 list f#

我有一个程序,如果我输入“tac Text.txt”(或引号中的任何文件路径,但这是我制作/使用的测试文本文件),它将打印txt<中的内容 文件,但相反。但它不能正常工作。它应该出现在哪里

No it is not.


Yes-it is.
No it is not.
Hello  my  name  is  Jim.

它显示为

["No it is not."; ""; ""; "Yes-it is."; "No it is not.";  "Hello  my 
name  is  Jim."].

我的代码目前是

open System
open System.IO

let countLines path = 
    File.ReadAllLines(path) |> Seq.toList|> List.rev



    // File.ReadAllLines reads the file, turns it into a list then reverses the list.


let printFunction lines = 
    printfn "%A" lines

    // This, when called, will print the file in reverse. 

let tac path =
    if File.Exists(path) then
        let lines = countLines path
        printFunction lines
    else
        printfn "File not found."

[<EntryPoint>]
let main argv = 
    if argv.Length > 0 then
        tac argv.[0] 
    else
        printfn "Error - Please enter file path."
    0

我假设这是由于转换为列表,有没有办法可以正常打印它?我希望这只是我犯的一个新手小错误。

更新:我刚刚改变了

let countLines path = 
File.ReadAllLines(path) |> Seq.toList|> List.rev

let countLines path = 
File.ReadAllLines(path) |> Array.rev

发生了同样的事情,但我希望它能让我更接近我想要的结果。

最佳答案

它正在写出您的整个数据类型,这是一个字符串列表

您需要遍历列表:

let printFunction lines = 
    for line in lines do printfn "%s" line

关于list - F# - 创建程序来读取文件并反向打印,如何删除所有新行引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594336/

相关文章:

Python 截断切片赋值

python - Python list.pop() 的 lua 等价物是什么?

f# - 为什么我可以把 |> 放在下一行但是 <|给出语法错误?

inheritance - F#中的类型继承

python - 重叠片段

python - 如何拆分 Python 列表并按升序创建新列表?

algorithm - 实现极小极大算法

f# - F# 的 Web 项目

f# - 在 F# 中计算列表中未更改的元素数

java - 从一个文件中获取整数列表