Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: John Prevost <visigoth@cs.cmu.edu>
To: Chris Hecker <checker@d6.com>
Cc: "Jérôme Marant" <jerome.marant@fr.thalesgroup.com>,
	caml-list@inria.fr,
	"Daniel de Rauglaudre" <daniel.de_rauglaudre@inria.fr>
Subject: Re: [Caml-list] How to read three integers from a text-file... ?
Date: 25 Apr 2002 16:52:22 -0400	[thread overview]
Message-ID: <86lmbbtsah.fsf@laurelin.dementia.org> (raw)
In-Reply-To: <4.3.2.7.2.20020425105510.02d0a600@mail.d6.com>

>>>>> "ch" == Chris Hecker <checker@d6.com> writes:

    ch> As Markus says, the Printf.printf doesn't work in this case
    ch> anyway right now, since it happens completely at compile time,
    ch> I believe.

Actually, this is not the case, though printf formats are limited in
what you can do with them.  (i.e. you may substitute, but not
combine.)

As an example:

# let fmt x = (x : ('a,'b,'c) format)

  let a = fmt "First: %d, Second: %d\n"
  let b = fmt "[1, %d; 2, %d]\n"
  let f fmt = Printf.printf fmt 10 10
  let _ = f a
  let _ = f b
  ;;

First: 10, Second: 10
[1, 10; 2, 10]
val fmt : ('a, 'b, 'c) format -> ('a, 'b, 'c) format = <fun>
val a : (int -> int -> unit, out_channel, unit) format = <abstr>
val b : (int -> int -> unit, out_channel, unit) format = <abstr>
val f : (int -> int -> 'a, out_channel, unit) format -> 'a = <fun>

The definition of fmt is just to provide a quick way to force a string
into the context of a format type--otherwise, the cimpiler would
assign the type string to that value.

So it is possible to do some small things with these formats.  A more
interesting strategy is to use formatting combinators:

let id x = x
let ( ** ) f g x = f (g x)

let str     k s x = k (s ^ x)
let int     k s x = k (s ^ string_of_int x)
let lit p x k s   = p k s x

let lis p   k s x = match x with
  | [] -> k (s ^ "[]")
  | _  ->
    let rec loop xs k s = match xs with
      | [x]   -> p (fun s -> k (s ^ "]")) s x
      | x::xs -> p (fun s -> loop xs k (s ^ ";")) s x
    in loop x k (s ^ "[")

let format p = p id ""

Which lets you do things like:

# format (int ** lit int 3) 5;;
- : string = "53"
# format (int ** lit str " -> " ** lis int) 5 [1;2;3;4;5];;
- : string = "5 -> [1;2;3;4;5]"

Of course, these combinators can be extended for other ways of
producing results, and you can add new formats later.  So, it's more
extensible than the current printf is.  Quite nice, really.

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


  reply	other threads:[~2002-04-26 14:55 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-23 10:41 Jacek Chrzaszcz
2002-04-24 10:44 ` Stefano Lanzavecchia
2002-04-24 18:46   ` Tomasz Zielonka
2002-04-24 11:16 ` Jacques Garrigue
2002-04-24 13:40   ` Tomasz Zielonka
2002-04-25  5:30   ` pervasives (was: Re: [Caml-list] How to read three integers from a text-file... ?) Chris Hecker
2002-04-25  6:33     ` Tomasz Zielonka
2002-04-25 17:54       ` Chris Hecker
2002-04-27  4:43         ` John Max Skaller
2002-04-27 16:02           ` [Caml-list] input_line (Re: pervasives) Lauri Alanko
2002-04-30 12:07             ` [Caml-list] input_line Xavier Leroy
2002-05-03  0:13               ` Lauri Alanko
2002-05-03 11:27                 ` Florian Hars
2002-04-24 21:23 ` [Caml-list] How to read three integers from a text-file... ? Tomasz Zielonka
2002-04-25  1:51   ` John Max Skaller
2002-04-25  8:55   ` Daniel de Rauglaudre
2002-04-25 11:19     ` Markus Mottl
2002-04-25 11:33       ` Jérôme Marant
2002-04-25 11:43         ` Markus Mottl
2002-04-25 17:56         ` Chris Hecker
2002-04-25 20:52           ` John Prevost [this message]
2002-04-25 23:32           ` Jacques Garrigue
2002-04-26  7:25             ` Jérôme Marant
2002-04-26 12:16           ` Jacques Garrigue
2002-05-02  8:48             ` Jacques Garrigue
2002-04-26  1:39         ` Daniel de Rauglaudre
2002-04-29  6:44   ` Francois Pottier
2002-04-30 11:07     ` Dave Berry
2002-04-30 12:20       ` Francois Pottier
2002-04-30 13:54         ` T. Kurt Bond
2002-05-03 22:12         ` Dave Berry
2002-04-30 14:42       ` Jocelyn Sérot
2002-05-02  7:34         ` [Caml-list] Extensible tuple types Francois Pottier
2002-05-02  9:42           ` Alain Frisch
2002-05-02 11:03             ` Francois Pottier
     [not found]       ` <6ECF4649-5C48-11D6-AC27-0003934491C2@lasmea.univ-bpclermon t.fr>
2002-05-03 21:58         ` [Caml-list] How to read three integers from a text-file... ? Dave Berry
2002-05-06  0:53           ` Eray Ozkural
2002-05-06  6:40           ` Florian Hars
2002-04-30 23:30     ` [Caml-list] Danvy "Functional Unparsing" style output in OCaml [was: How to read three integers from a text-file... ?] T. Kurt Bond
2002-05-13 14:11       ` [Caml-list] RE: Danvy "Functional Unparsing" style output in OCaml T. Kurt Bond
2002-05-13 19:59         ` [Caml-list] "Functional Unparsing" benchmark results links fixed [Was: Danvy "Functional Unparsing" style output in OCaml] T. Kurt Bond

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86lmbbtsah.fsf@laurelin.dementia.org \
    --to=visigoth@cs.cmu.edu \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.com \
    --cc=daniel.de_rauglaudre@inria.fr \
    --cc=jerome.marant@fr.thalesgroup.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox