4 Programmers coming from C or Java often ask if
5 <:StandardML:Standard ML> has a `printf` function. It does not.
6 However, it is possible to implement your own version with only a few
9 Here is a definition for `printf` and `fprintf`, along with format
10 specifiers for booleans, integers, and reals.
16 fun $ (_, f) = f (fn p => p ()) ignore
17 fun fprintf out f = f (out, id)
18 val printf = fn z => fprintf TextIO.stdOut z
19 fun one ((out, f), make) g =
23 r (fn () => (p (); TextIO.output (out, s))))))
24 fun ` x s = one (x, fn f => f s)
25 fun spec to x = one (x, fn f => f o to)
26 val B = fn z => spec Bool.toString z
27 val I = fn z => spec Int.toString z
28 val R = fn z => spec Real.toString z
32 Here's an example use.
36 val () = printf `"Int="I`" Bool="B`" Real="R`"\n" $ 1 false 2.0
39 This prints the following.
42 Int=1 Bool=false Real=2.0
45 In general, a use of `printf` looks like
48 printf <spec1> ... <specn> $ <arg1> ... <argm>
51 where each `<speci>` is either a specifier like `B`, `I`, or `R`, or
52 is an inline string, like ++`"foo"++. A backtick (+`+)
53 must precede each inline string. Each `<argi>` must be of the
54 appropriate type for the corresponding specifier.
56 SML `printf` is more powerful than its C counterpart in a number of
57 ways. In particular, the function produced by `printf` is a perfectly
58 ordinary SML function, and can be passed around, used multiple times,
63 val f: int -> bool -> unit = printf `"Int="I`" Bool="B`"\n" $
68 The definition of `printf` is even careful to not print anything until
69 it is fully applied. So, examples like the following will work as
73 val f: int -> bool -> unit = printf `"Int="I`" Bool="B`"\n" $ 13
78 It is also easy to define new format specifiers. For example, suppose
79 we wanted format specifiers for characters and strings.
82 val C = fn z => spec Char.toString z
83 val S = fn z => spec (fn s => s) z
86 One can define format specifiers for more complex types, e.g. pairs of
93 concat ["(", Int.toString i, ", ", Int.toString j, ")"])
97 Here's an example use.
100 val () = printf `"Test "I2`" a string "S`"\n" $ (1, 2) "hello"
104 == Printf via <:Fold:> ==
106 `printf` is best viewed as a special case of variable-argument
107 <:Fold:> that inductively builds a function as it processes its
108 arguments. Here is the definition of a `Printf` structure in terms of
109 fold. The structure is equivalent to the above one, except that it
110 uses the standard `$` instead of a specialized one.
117 Fold.fold ((out, id), fn (_, f) => f (fn p => p ()) ignore)
119 val printf = fn z => fprintf TextIO.stdOut z
121 fun one ((out, f), make) =
125 r (fn () => (p (); TextIO.output (out, s))))))
128 fn z => Fold.step1 (fn (s, x) => one (x, fn f => f s)) z
130 fun spec to = Fold.step0 (fn x => one (x, fn f => f o to))
132 val B = fn z => spec Bool.toString z
133 val I = fn z => spec Int.toString z
134 val R = fn z => spec Real.toString z
138 Viewing `printf` as a fold opens up a number of possibilities. For
139 example, one can name parts of format strings using the fold idiom for
140 naming sequences of steps.
143 val IB = fn u => Fold.fold u `"Int="I`" Bool="B
144 val () = printf IB`" "IB`"\n" $ 1 true 3 false
147 One can even parametrize over partial format strings.
150 fun XB X = fn u => Fold.fold u `"X="X`" Bool="B
151 val () = printf (XB I)`" "(XB R)`"\n" $ 1 true 2.0 false
158 * <!Cite(Danvy98, Functional Unparsing)>