OCaml Format and Structural Fields: Why is my output not following the example?

There is an example on the Use Format Module page on the OCaml website, which gives an example of the differences between structural and packing cells. I am trying to reproduce this example. (I am using OCaml 3.12.1.)

With the following input:

Format.printf "@[<hov 2>( ---@ \ n@ [<hov 2>( ---@ \ n@ [<hov 2>( ---@ ,)@]@,)@]@,)@]@\n" 

I get the expected result:

 (--- (--- (---))) 

What I cannot understand is how to get the output that the page ascribes to the "building blocks":

 (--- (--- (--- ) ) ) 

I was not sure which Format.printf identifier maps to these fields, so I tried several options:

 Format.printf "@[<hv 2>( ---@ \ n@ [<hv 2>( ---@ \ n@ [<hv 2>( ---@ ,)@]@,)@]@,)@]@\n" Format.printf "@[<2>( ---@ \ n@ [<2>( ---@ \ n@ [<2>( ---@ ,)@]@,)@]@,)@]@\n" Format.printf "@[<b 2>( ---@ \ n@ [<b 2>( ---@ \ n@ [<b 2>( ---@ ,)@]@,)@]@,)@]@\n" 

But all of the above gives the same result as the original <hov 2> example. Does anyone have any ideas how I can get a result similar to the second example from a web page?

+6
source share
1 answer

I would think that your second example (with hv-boxes) should work. But it looks like @ \ n does not have the expected behavior.

Solution 1: forces a line break before the right bracket, i.e. replaces @ with @ \ n.

 let () = Format.printf "@."; Format.printf "@[<hov 2>( ---@ \ n@ [<hov 2>( ---@ \ n@ [<hov 2>( ---@ \n)@]@\n)@]@\n)@]@\n"; Format.printf "@." 

Result:

 (--- (--- (--- ) ) ) 

Problem: there will always be a line break, it will never print:

(--- (--- (---)))

even if he has a place for him. If this is a problem for you, see Solution 2.

Solution 2: Use longer lines :) If the break is caused by a long line, then the hv block breaks the line before the closing bracket.

 let () = Format.printf "@."; Format.printf "@[<hv 2>(--------- ---------------------------------------------------------------@ ,@[<hv 2>( ---@ ,@[<hv 2>( ---@ ,)@]@,)@]@,)@]"; Format.printf "@." 

Result:

 (------------------------------------------------------------------------ (---(---)) ) 

However, the closing bracket does not match the open.

Solution 3: If you want the right bracket to be aligned with the left, you need two boxes.

 let () = Format.printf "@."; Format.printf "@[<hv>@[<hv 2>(------------------------------------------------------------------------\ @[<hv>@[<hv 2>(------------------------------------------------------------------------\ @[<hv>@[<hv 2>(------------------------------------------------------------------------\ @]@,)@]@]@,)@]@]@,)@]"; Format.printf "@." 

Result:

 (------------------------------------------------------------------------ (------------------------------------------------------------------------ (------------------------------------------------------------------------ ) ) ) 
+7
source

Source: https://habr.com/ru/post/926031/


All Articles