在 F# 中打印大型列表
我正在尝试使用 F# 打印一个大列表,但遇到了困难。我正在尝试在 F# 中创建一个词法分析器,我相信我已经完成了,但我似乎无法让它打印整个列表来检查它。
这是我尝试执行的操作的示例
let modifierReg = Regex("(public|private)");
let isModifier str = if (modifierReg.IsMatch(str)) then ["Modifier"; str] else ["Keyword"; str]
let readLines filePath = seq {
use sr = new StreamReader (filePath:string)
while not sr.EndOfStream do
yield sr.ReadLine () }
let splitLines listArray =
listArray
|> Seq.map (fun (line: string) -> let m = Regex.Match(line, commentReg) in if m.Success then (m.Groups.Item 1).Value.Split([|' '|], System.StringSplitOptions.RemoveEmptyEntries) else line.Split([|' '|], System.StringSplitOptions.RemoveEmptyEntries) )
let res =
string1
|> readLines
|> splitLines
let scanLines lexicons =
lexicons
|> Seq.map (fun strArray -> strArray |> Seq.map (fun str -> isModifier(str)))
let printSeq seq =
printfn "%A" seq
let scanner filePath =
filePath
|> readLines
|> splitLines
|> scanLines
let scannerResults = scanner pathToCode
printSeq scannerResults
当我尝试打印列表时,我得到以下信息
序列 [seq [["关键字"; “班级”]; [“标识符”; “一个”]]; seq [["块"; “{”]]; seq [["修饰符"; “民众”]; [“类型”; “整数”]; [“标识符”; “x;”]]; seq [["修饰符"; “民众”]; [“类型”; “整数”]; [“标识符”; “y;”]]; ...]
我无法再打印它。我得到了同样的行为,就像下面这样简单,
printfn "%a" [1 .. 101]]
我似乎不知道如何打印它。有人有这方面的经验吗?我似乎找不到任何例子
I am trying to print a large list with F# and am a difficult time. I am trying to create a lexical analyzer in F# I believe I am done but I can't seem to get it to print the entire list to check it.
here is an example of what I am trying to do
let modifierReg = Regex("(public|private)");
let isModifier str = if (modifierReg.IsMatch(str)) then ["Modifier"; str] else ["Keyword"; str]
let readLines filePath = seq {
use sr = new StreamReader (filePath:string)
while not sr.EndOfStream do
yield sr.ReadLine () }
let splitLines listArray =
listArray
|> Seq.map (fun (line: string) -> let m = Regex.Match(line, commentReg) in if m.Success then (m.Groups.Item 1).Value.Split([|' '|], System.StringSplitOptions.RemoveEmptyEntries) else line.Split([|' '|], System.StringSplitOptions.RemoveEmptyEntries) )
let res =
string1
|> readLines
|> splitLines
let scanLines lexicons =
lexicons
|> Seq.map (fun strArray -> strArray |> Seq.map (fun str -> isModifier(str)))
let printSeq seq =
printfn "%A" seq
let scanner filePath =
filePath
|> readLines
|> splitLines
|> scanLines
let scannerResults = scanner pathToCode
printSeq scannerResults
When I try to print the list I get the following
seq
[seq [["Keyword"; "class"]; ["Identifier"; "A"]]; seq [["Block"; "{"]];
seq [["Modifier"; "public"]; ["Type"; "int"]; ["Identifier"; "x;"]];
seq [["Modifier"; "public"]; ["Type"; "int"]; ["Identifier"; "y;"]]; ...]
I can't get it to print any further. I get the same behavior with something as simple as the following
printfn "%a" [1 .. 101]]
I can't seem to figure out how to print it off. Anyone have any experience with this? I can't seem to find any examples
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Seq.iter
将迭代序列的所有元素,因此例如将打印每个元素。 (“%A”说明符擅长打印任意数据的常见情况,但对于大型列表或其他内容,您可以进行更精细的控制,如下所示,通过迭代每个元素并单独打印每个元素,例如在新行上多于。)
Seq.iter
will iterate over all the elements of a sequence, so e.g.will print each of the elements. (The "%A" specifier is good at the common case for printing arbitrary data, but for large lists or whatnot, you can exercise finer control, as here, by iterating over every element and printing each individually, e.g. on a new line as above.)
您不是在处理列表,而是在处理序列。由于序列可能是无限的,
printf
和朋友只输出前 N 个元素。有道理。You're not working with lists, you're working with sequences. Since sequences may be infinite,
printf
and friends only output the first N elements. Makes sense.布莱恩和丹尼尔已经回答了你的问题。我想补充一点,
%A
将使用反射来打印传递给printfn
函数的对象。在您的情况下,它不是简单的项目列表,而是列表列表的列表等等,它基本上是一棵树。如果这棵树太大,那么printfn "%A"
会造成性能问题,您需要编写自己的打印函数来遍历树并打印它。Brain and Daniel has already answered your question. I would add that
%A
would use reflection to print the object passed toprintfn
function. In your case it is not simple list of items but rather a list of list of list and so on, which basically is a tree. If this tree is too large thenprintfn "%A"
would be pose a performance problem and you would need to write your own print function that could traverse the tree and print it.