-
Notifications
You must be signed in to change notification settings - Fork 1
/
printf.sml
37 lines (26 loc) · 923 Bytes
/
printf.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
structure Printf =
struct
fun $ (a, f) = f a
fun id x = x
structure Fold =
struct
fun fold (a, f) g = g (a, f)
fun step0 h (a, f) = fold (h a, f)
fun step1 h (a, f) b = fold (h (b, a), f)
end
fun fprintf out =
Fold.fold ((out, id), fn (_, f) => f (fn p => p ()) ignore)
val printf = fn z => fprintf TextIO.stdOut z
fun one ((out, f), make) =
(out, fn r =>
f (fn p =>
make (fn s =>
r (fn () => (p (); TextIO.output (out, s))))))
val ` =
fn z => Fold.step1 (fn (s, x) => one (x, fn f => f s)) z
fun spec to = Fold.step0 (fn x => one (x, fn f => f o to))
val B = fn z => spec Bool.toString z
val I = fn z => spec Int.toString z
val R = fn z => spec Real.toString z
fun test () = printf `"Int="I`" Bool="B`" Real="R`"\n" $ 1 false 2.0
end