gpt4 book ai didi

formatting - Format.fprintf 中框的缩进

转载 作者:行者123 更新时间:2023-12-02 08:22:47 27 4
gpt4 key购买 nike

请考虑函数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 ();
()

它递归地打开 hovboxes 并打印一些东西,然后是换行符提示 (@.)。当我调用 f 3 时,我得到以下输出:

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

但我预计:

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

您能解释一下为什么我获得第一个输出以及我需要更改什么才能获得第二个输出吗?

最佳答案

@. 不是换行提示,它等同于 print_newline这叫print_flush关闭所有打开的框并在其后跟一个新行。

如果你想用 Format 逐行打印,你应该用 open_vbox 打开一个垂直框并使用 print_cut ("@,") 每当你想输出一个新行时。

关于formatting - Format.fprintf 中框的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35275033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com