Formatting 格式为.fprintf的方框缩进 请考虑函数f: open Format let rec f i = match i with | x when x <= 0 -> () | i -> pp_open_hovbox std_formatter 2; printf "This is line %d@." i; f (i-1); printf "This is line %d@." i; close_box (); ()

Formatting 格式为.fprintf的方框缩进 请考虑函数f: open Format let rec f i = match i with | x when x <= 0 -> () | i -> pp_open_hovbox std_formatter 2; printf "This is line %d@." i; f (i-1); printf "This is line %d@." i; close_box (); (),formatting,format,ocaml,Formatting,Format,Ocaml,但我希望: This is line 3 This is line 2 This is line 1 This is line 1 This is line 2 This is line 3 你能解释一下为什么我会获得第一个输出,以及我需要做哪些更改才能获得第二个输出吗?@。不是换行提示,它相当于关闭所有打开的框并在其后换行的调用 如果你想用格式逐行打印,你应该打开一个垂直框,在你想输出新行时使用(“@,)。而不是使用@。你应该使用@\nspecificator。前者将

但我希望:

This is line 3
  This is line 2
    This is line 1
    This is line 1
  This is line 2
This is line 3

你能解释一下为什么我会获得第一个输出,以及我需要做哪些更改才能获得第二个输出吗?

@。
不是换行提示,它相当于关闭所有打开的框并在其后换行的调用


如果你想用
格式逐行打印,你应该打开一个垂直框,在你想输出新行时使用(
“@,
)。

而不是使用
@。
你应该使用
@\n
specificator。前者将刷新格式化程序并输出一个硬换行符,实际上破坏了漂亮的打印。它打算在文档末尾使用,而且,由于它实际上是不可组合的,所以我要警告不要使用它

使用
@\n
,您将获得更接近预期的输出:

This is line 3
  This is line 2
    This is line 1
      This is line 1
      This is line 2
    This is line 3

顺便说一句,使用
vbox
并发射
@可以获得相同的输出好的中断提示,这样更好。

谢谢,我没意识到打印刷新会关闭框。也谢谢!有没有办法在不添加
@\n
的情况下获取第一行的缩进?
This is line 3
  This is line 2
    This is line 1
      This is line 1
      This is line 2
    This is line 3