| 1 |
= Printf |
| 2 |
|
| 3 |
Programmers coming from C or Java often ask if |
| 4 |
<<StandardML#,Standard ML>> has a `printf` function. It does not. |
| 5 |
However, it is possible to implement your own version with only a few |
| 6 |
lines of code. |
| 7 |
|
| 8 |
Here is a definition for `printf` and `fprintf`, along with format |
| 9 |
specifiers for booleans, integers, and reals. |
| 10 |
|
| 11 |
[source,sml] |
| 12 |
---- |
| 13 |
structure Printf = |
| 14 |
struct |
| 15 |
fun $ (_, f) = f (fn p => p ()) ignore |
| 16 |
fun fprintf out f = f (out, id) |
| 17 |
val printf = fn z => fprintf TextIO.stdOut z |
| 18 |
fun one ((out, f), make) g = |
| 19 |
g (out, fn r => |
| 20 |
f (fn p => |
| 21 |
make (fn s => |
| 22 |
r (fn () => (p (); TextIO.output (out, s)))))) |
| 23 |
fun ` x s = one (x, fn f => f s) |
| 24 |
fun spec to x = one (x, fn f => f o to) |
| 25 |
val B = fn z => spec Bool.toString z |
| 26 |
val I = fn z => spec Int.toString z |
| 27 |
val R = fn z => spec Real.toString z |
| 28 |
end |
| 29 |
---- |
| 30 |
|
| 31 |
Here's an example use. |
| 32 |
|
| 33 |
[source,sml] |
| 34 |
---- |
| 35 |
val () = printf `"Int="I`" Bool="B`" Real="R`"\n" $ 1 false 2.0 |
| 36 |
---- |
| 37 |
|
| 38 |
This prints the following. |
| 39 |
|
| 40 |
---- |
| 41 |
Int=1 Bool=false Real=2.0 |
| 42 |
---- |
| 43 |
|
| 44 |
In general, a use of `printf` looks like |
| 45 |
|
| 46 |
---- |
| 47 |
printf <spec1> ... <specn> $ <arg1> ... <argm> |
| 48 |
---- |
| 49 |
|
| 50 |
where each `<speci>` is either a specifier like `B`, `I`, or `R`, or |
| 51 |
is an inline string, like `{backtick}"foo"`. A backtick (`{backtick}`) |
| 52 |
must precede each inline string. Each `<argi>` must be of the |
| 53 |
appropriate type for the corresponding specifier. |
| 54 |
|
| 55 |
SML `printf` is more powerful than its C counterpart in a number of |
| 56 |
ways. In particular, the function produced by `printf` is a perfectly |
| 57 |
ordinary SML function, and can be passed around, used multiple times, |
| 58 |
etc. For example: |
| 59 |
|
| 60 |
[source,sml] |
| 61 |
---- |
| 62 |
val f: int -> bool -> unit = printf `"Int="I`" Bool="B`"\n" $ |
| 63 |
val () = f 1 true |
| 64 |
val () = f 2 false |
| 65 |
---- |
| 66 |
|
| 67 |
The definition of `printf` is even careful to not print anything until |
| 68 |
it is fully applied. So, examples like the following will work as |
| 69 |
expected. |
| 70 |
|
| 71 |
[source,sml] |
| 72 |
---- |
| 73 |
val f: int -> bool -> unit = printf `"Int="I`" Bool="B`"\n" $ 13 |
| 74 |
val () = f true |
| 75 |
val () = f false |
| 76 |
---- |
| 77 |
|
| 78 |
It is also easy to define new format specifiers. For example, suppose |
| 79 |
we wanted format specifiers for characters and strings. |
| 80 |
|
| 81 |
[source,sml] |
| 82 |
---- |
| 83 |
val C = fn z => spec Char.toString z |
| 84 |
val S = fn z => spec (fn s => s) z |
| 85 |
---- |
| 86 |
|
| 87 |
One can define format specifiers for more complex types, e.g. pairs of |
| 88 |
integers. |
| 89 |
|
| 90 |
[source,sml] |
| 91 |
---- |
| 92 |
val I2 = |
| 93 |
fn z => |
| 94 |
spec (fn (i, j) => |
| 95 |
concat ["(", Int.toString i, ", ", Int.toString j, ")"]) |
| 96 |
z |
| 97 |
---- |
| 98 |
|
| 99 |
Here's an example use. |
| 100 |
|
| 101 |
[source,sml] |
| 102 |
---- |
| 103 |
val () = printf `"Test "I2`" a string "S`"\n" $ (1, 2) "hello" |
| 104 |
---- |
| 105 |
|
| 106 |
|
| 107 |
== Printf via <<Fold#>> |
| 108 |
|
| 109 |
`printf` is best viewed as a special case of variable-argument |
| 110 |
<<Fold#>> that inductively builds a function as it processes its |
| 111 |
arguments. Here is the definition of a `Printf` structure in terms of |
| 112 |
fold. The structure is equivalent to the above one, except that it |
| 113 |
uses the standard `$` instead of a specialized one. |
| 114 |
|
| 115 |
[source,sml] |
| 116 |
---- |
| 117 |
structure Printf = |
| 118 |
struct |
| 119 |
fun fprintf out = |
| 120 |
Fold.fold ((out, id), fn (_, f) => f (fn p => p ()) ignore) |
| 121 |
|
| 122 |
val printf = fn z => fprintf TextIO.stdOut z |
| 123 |
|
| 124 |
fun one ((out, f), make) = |
| 125 |
(out, fn r => |
| 126 |
f (fn p => |
| 127 |
make (fn s => |
| 128 |
r (fn () => (p (); TextIO.output (out, s)))))) |
| 129 |
|
| 130 |
val ` = |
| 131 |
fn z => Fold.step1 (fn (s, x) => one (x, fn f => f s)) z |
| 132 |
|
| 133 |
fun spec to = Fold.step0 (fn x => one (x, fn f => f o to)) |
| 134 |
|
| 135 |
val B = fn z => spec Bool.toString z |
| 136 |
val I = fn z => spec Int.toString z |
| 137 |
val R = fn z => spec Real.toString z |
| 138 |
end |
| 139 |
---- |
| 140 |
|
| 141 |
Viewing `printf` as a fold opens up a number of possibilities. For |
| 142 |
example, one can name parts of format strings using the fold idiom for |
| 143 |
naming sequences of steps. |
| 144 |
|
| 145 |
[source,sml] |
| 146 |
---- |
| 147 |
val IB = fn u => Fold.fold u `"Int="I`" Bool="B |
| 148 |
val () = printf IB`" "IB`"\n" $ 1 true 3 false |
| 149 |
---- |
| 150 |
|
| 151 |
One can even parametrize over partial format strings. |
| 152 |
|
| 153 |
[source,sml] |
| 154 |
---- |
| 155 |
fun XB X = fn u => Fold.fold u `"X="X`" Bool="B |
| 156 |
val () = printf (XB I)`" "(XB R)`"\n" $ 1 true 2.0 false |
| 157 |
---- |
| 158 |
|
| 159 |
|
| 160 |
== Also see |
| 161 |
|
| 162 |
* <<PrintfGentle#>> |
| 163 |
* <<References#Danvy98,Functional Unparsing>> |