caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Format questions
@ 2017-04-08 20:52 Matej Kosik
  2017-04-08 22:24 ` Josh Berdine
  0 siblings, 1 reply; 4+ messages in thread
From: Matej Kosik @ 2017-04-08 20:52 UTC (permalink / raw)
  To: caml-list

Hi everybody,

I am trying to understand the concepts in the Format module.

While reading this:

  https://ocaml.org/learn/tutorials/format.html

One of the mini experiments I did was:

  set_margin 11;
  open_hvbox 0;
  print_string "---";
  print_space ();
  print_string "---";
  print_space ();
  print_string "---";
  print_newline ();
  print_newline ();

I've got the expected output, which is:

  ---
  ---
  ---

No surprises.

However, when I tried to write express the same intentions via Format.fprintf function:

  Format.set_margin 11;
  Format.fprintf std_formatter "@[<hv 0>---@ ---@ ---@.@.";

I get:

  --- --- ---

I'd like to ask for some clues as to why the output of the above Format.fprintf is different from the more verbose version above.

(this is the one module that, to this day, seems a bit mysterious to me --- that is why I ask)

Thanks in advance for the help.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Format questions
  2017-04-08 20:52 [Caml-list] Format questions Matej Kosik
@ 2017-04-08 22:24 ` Josh Berdine
  2017-04-09  7:44   ` Matej Kosik
  0 siblings, 1 reply; 4+ messages in thread
From: Josh Berdine @ 2017-04-08 22:24 UTC (permalink / raw)
  To: Matej Kosik, caml-list

On Sat, Apr 08 2017, Matej Kosik wrote:

> Hi everybody,
>
> I am trying to understand the concepts in the Format module.
>
> While reading this:
>
>   https://ocaml.org/learn/tutorials/format.html
>
> One of the mini experiments I did was:
>
>   set_margin 11;
>   open_hvbox 0;
>   print_string "---";
>   print_space ();
>   print_string "---";
>   print_space ();
>   print_string "---";
>   print_newline ();
>   print_newline ();
>
> I've got the expected output, which is:
>
>   ---
>   ---
>   ---
>
> No surprises.
>
> However, when I tried to write express the same intentions via Format.fprintf function:
>
>   Format.set_margin 11;
>   Format.fprintf std_formatter "@[<hv 0>---@ ---@ ---@.@.";
>
> I get:
>
>   --- --- ---
>
> I'd like to ask for some clues as to why the output of the above Format.fprintf is different from the more verbose version above.

Are you, by chance, evaluating this in utop (which seems to mess with std_formatter)?   I see your expected behavior using the standard toplevel.  (After adding Format. to std_formatter)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Format questions
  2017-04-08 22:24 ` Josh Berdine
@ 2017-04-09  7:44   ` Matej Kosik
  2017-04-09  7:45     ` Nicolás Ojeda Bär
  0 siblings, 1 reply; 4+ messages in thread
From: Matej Kosik @ 2017-04-09  7:44 UTC (permalink / raw)
  To: Josh Berdine, caml-list

Hi Josh,

On 04/09/2017 12:24 AM, Josh Berdine wrote:
> On Sat, Apr 08 2017, Matej Kosik wrote:
> 
>> Hi everybody,
>>
>> I am trying to understand the concepts in the Format module.
>>
>> While reading this:
>>
>>   https://ocaml.org/learn/tutorials/format.html
>>
>> One of the mini experiments I did was:
>>
>>   set_margin 11;
>>   open_hvbox 0;
>>   print_string "---";
>>   print_space ();
>>   print_string "---";
>>   print_space ();
>>   print_string "---";
>>   print_newline ();
>>   print_newline ();
>>
>> I've got the expected output, which is:
>>
>>   ---
>>   ---
>>   ---
>>
>> No surprises.
>>
>> However, when I tried to write express the same intentions via Format.fprintf function:
>>
>>   Format.set_margin 11;
>>   Format.fprintf std_formatter "@[<hv 0>---@ ---@ ---@.@.";
>>
>> I get:
>>
>>   --- --- ---
>>
>> I'd like to ask for some clues as to why the output of the above Format.fprintf is different from the more verbose version above.
> 
> Are you, by chance, evaluating this in utop (which seems to mess with std_formatter)?   I see your expected behavior using the standard toplevel.  (After adding Format. to std_formatter)

I was using standard Ocaml toplevel
(in a context where I opened Format module).

I looked at it today and I have realized that instead of this:

  set_margin 11;

if I want to use "Format.fprintf std_formatter" I should do

  pp_set_margin std_formatter 11;

as if

  set_margin <> pp_set_margin std_formatter

That I did not know.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Format questions
  2017-04-09  7:44   ` Matej Kosik
@ 2017-04-09  7:45     ` Nicolás Ojeda Bär
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolás Ojeda Bär @ 2017-04-09  7:45 UTC (permalink / raw)
  To: Matej Kosik; +Cc: Josh Berdine, caml-list

[-- Attachment #1: Type: text/plain, Size: 2112 bytes --]

Hi Matej,

I do not think that is the reason, as both calls do the same thing, see

  https://github.com/ocaml/ocaml/blob/trunk/stdlib/format.ml#L1153

Cheers,
Nicolas


On Sun, Apr 9, 2017 at 9:44 AM, Matej Kosik <
5764c029b688c1c0d24a2e97cd764f@gmail.com> wrote:

> Hi Josh,
>
> On 04/09/2017 12:24 AM, Josh Berdine wrote:
> > On Sat, Apr 08 2017, Matej Kosik wrote:
> >
> >> Hi everybody,
> >>
> >> I am trying to understand the concepts in the Format module.
> >>
> >> While reading this:
> >>
> >>   https://ocaml.org/learn/tutorials/format.html
> >>
> >> One of the mini experiments I did was:
> >>
> >>   set_margin 11;
> >>   open_hvbox 0;
> >>   print_string "---";
> >>   print_space ();
> >>   print_string "---";
> >>   print_space ();
> >>   print_string "---";
> >>   print_newline ();
> >>   print_newline ();
> >>
> >> I've got the expected output, which is:
> >>
> >>   ---
> >>   ---
> >>   ---
> >>
> >> No surprises.
> >>
> >> However, when I tried to write express the same intentions via
> Format.fprintf function:
> >>
> >>   Format.set_margin 11;
> >>   Format.fprintf std_formatter "@[<hv 0>---@ ---@ ---@.@.";
> >>
> >> I get:
> >>
> >>   --- --- ---
> >>
> >> I'd like to ask for some clues as to why the output of the above
> Format.fprintf is different from the more verbose version above.
> >
> > Are you, by chance, evaluating this in utop (which seems to mess with
> std_formatter)?   I see your expected behavior using the standard
> toplevel.  (After adding Format. to std_formatter)
>
> I was using standard Ocaml toplevel
> (in a context where I opened Format module).
>
> I looked at it today and I have realized that instead of this:
>
>   set_margin 11;
>
> if I want to use "Format.fprintf std_formatter" I should do
>
>   pp_set_margin std_formatter 11;
>
> as if
>
>   set_margin <> pp_set_margin std_formatter
>
> That I did not know.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 3622 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-04-09  7:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-08 20:52 [Caml-list] Format questions Matej Kosik
2017-04-08 22:24 ` Josh Berdine
2017-04-09  7:44   ` Matej Kosik
2017-04-09  7:45     ` Nicolás Ojeda Bär

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).