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

时间:2017-01-11 15:10:46

标签: 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

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

2 个答案:

答案 0 :(得分:4)

它正在写出您的整个数据类型,即string list

您需要遍历列表:

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

答案 1 :(得分:3)

分别打印数组,列表或序列:

let printFunction = Seq.iter (printfn "%s")