caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Printing text with holes
@ 2003-09-29 13:16 Martin Jambon
  2003-09-29 14:10 ` Christian Lindig
                   ` (4 more replies)
  0 siblings, 5 replies; 24+ messages in thread
From: Martin Jambon @ 2003-09-29 13:16 UTC (permalink / raw)
  To: caml-list

Hello,

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
specifier'.
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):
  http://martin.jambon.free.fr/bazar/printfer.tar.gz


-- Martin


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2003-09-30 17:49 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-29 13:16 [Caml-list] Printing text with holes Martin Jambon
2003-09-29 14:10 ` Christian Lindig
2003-09-29 16:25 ` Benjamin Geer
2003-09-29 16:39 ` Brian Hurt
2003-09-29 17:45   ` Jean-Marc EBER
2003-09-29 18:22     ` Brian Hurt
2003-09-29 20:48       ` Pierre Weis
2003-09-29 21:52         ` Richard Jones
2003-09-29 22:07           ` Fred Yankowski
2003-09-29 22:14             ` Daniel M. Albro
2003-09-29 22:19               ` Richard Jones
2003-09-29 22:23             ` Matthieu Sozeau
2003-09-30  3:56             ` Brian Hurt
2003-09-30  4:40         ` Brian Hurt
2003-09-30  6:11           ` David Brown
2003-09-29 18:57     ` Martin Jambon
2003-09-29 21:25       ` Pierre Weis
2003-09-30 12:52       ` skaller
2003-09-30 17:48         ` Martin Jambon
2003-09-29 18:16   ` Richard Jones
2003-09-29 17:00 ` Pierre Weis
2003-09-29 17:03 ` Karl Zilles
2003-09-29 21:47   ` Pierre Weis
2003-09-30  7:20     ` Jean-Christophe Filliatre

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).