我有一个程序,如果我输入“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/