2 * Written by sweeks@sweeks
.com on
1999-08-31.
4 * A solution to mpuz
. (Try M
-x mpuz
in emacs
.)
5 * This solution is very loosely based on an OCAML solution posted to
6 * comp
.lang
.ml by Laurent Vaucher
<blo
.b@infonie
.fr
>.
9 (* override print so the benchmark is silent
*)
16 fun exists(l
, p
) = List.exists p l
18 fun map(l
, f
) = List.map f l
25 | x
:: l
=> loop(l
, f(x
, b
))
29 fun foreach(l
, f
) = fold(l
, (), fn (x
, ()) => f x
)
42 else loop(i
+ 1, f(String.sub(s
, i
), b
))
49 fun solve(a
, b
, c
, d
, e
) =
51 fun printNewline() = print
"\n"
53 val update
= Array
.update
57 ([a
, b
, c
, d
, e
], [], fn (s
, letters
) =>
59 (s
, letters
, fn (c
, letters
) =>
60 if List.exists(letters
, fn c
' => c
= c
')
65 Array
.array(Char.ord
Char.maxChar
+ 1, 0)
68 Array
.sub(letterValues
, ord c
)
70 fun setLetterValue(c
, v
) =
71 Array
.update(letterValues
, ord c
, v
)
74 String.fold(s
, 0, fn (c
, v
) => v
* 10 + letterValue c
)
79 print(concat
[String.str(c
), " = ",
80 Int.toString(letterValue(c
)), " "]))
85 val b0
= letterValue(String.sub(b
, 1))
86 val b1
= letterValue(String.sub(b
, 0))
95 andalso c
+ d
* 10 = e
100 val values
= List.map([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], fn v
=>
103 (* Try all assignments
of values to letters
. *)
109 (values
, fn (v
, r
) =>
113 ; setLetterValue(c
, v
)
124 Mpuz
.solve("AGH", "FB", "CBEE", "GHFD", "FGIJE")
127 * J
= 0 I
= 1 D
= 8 E
= 2 C
= 5 B
= 6 F
= 4 H
= 7 G
= 3 A
= 9