Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Printing text with holes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: [Caml-list] Printing text with holes

I am curious to know what people use to print long text written in a
natural language, and containing many holes, like dynamically generated
web pages.

Printf is not satisfying for this kind of task because the formatting
directives are far from the arguments: adding, removing or swapping
parameters in the text becomes really difficult with long text and several
arguments unless you make the text completely unreadable.

The solution I adopted some time ago is the following:

intead of this:

   printf "Hello %s %s! It is %i o'clock."
	first_name last_name time

we write:

   << Hello $first_name $last_name! It is $int{time} o'clock. >>

which is converted into

let _ =
  Pervasives.print_string "Hello ";
  Pervasives.print_string first_name;
  Pervasives.print_string " ";
  Pervasives.print_string last_name;
  Pervasives.print_string "! It is ";
  print_int time;
  Pervasives.print_string " o'clock."

Any function prefixed by print_ can be defined and used as a `format
It is also possible to pass several arguments to one function:
  << $x{arg1 arg2} >>
is equivalent to
  print_x arg1 arg2

but << ${"a" ^ "b"} >> becomes  print_string ("a" ^ "b")

<< $data.field >>            becomes  print_string data.field

Does anyone need this kind of approach?
Any better ideas?

I put my implementation here (it is quite ugly, I still ignore everything
about Camlp4):

-- Martin

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: