-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcountdown.ml
More file actions
479 lines (385 loc) · 10.5 KB
/
countdown.ml
File metadata and controls
479 lines (385 loc) · 10.5 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
(*
This is a solution to the countdown problem
in OCaml.
The countdown problem is described by Graham
Huttonin his Functional Pearls column of the
Journal of Functional Programming [1].
This code is a direct translation of the
Haskell solution given by Hutton in the same
column.
The Haskell solution makes heavy use of
list comprehensions. Since OCaml doesn't
have them, this code is at some places a
bit less elegant.
Example problem instance:
Numbers (1,3,7,10,25,50) with 765 should
yield:
(((25-7)-3)*(1+50))
((25-(3+7))*(1+50))
(((25-3)-7)*(1+50))
((25-10)*(1+50))
(3*((7*(50-10))-25))
[...]
Usage:
$ ocaml countdown.ml
or
$ ocamlc -o countdown countdown.ml
$ ./countdown
April 2005, Martijn Vermaat
This code is based on the work by Graham
Hutton and available as Open Source under
the new BSD License:
http://www.opensource.org/licenses/bsd-license.php
[1] Hutton, G. (2002) Functional Pearl: the
countdown problem. Journal of Functional
Programming.
http://www.cs.nott.ac.uk/~gmh/countdown.pdf
*)
(************************************************
Implementation
************************************************)
(*
Return a list of all permutations of al
sublists lf l.
*)
let subbags l =
(*
Return a list of all sublists of l.
*)
let rec sublists l = match l with
[] -> [[]]
| x::xs ->
(sublists xs) @
(List.map (fun l -> x::l) (sublists xs))
in
(*
Return a list of all possible lists resulting
from inserting e in l.
*)
let rec insertions e l = match l with
[] -> [[e]]
| x::xs -> (e::x::xs) :: (List.map (fun l -> x::l) (insertions e xs))
in
(*
Return a list of all permutations of l.
*)
let rec permutations l = match l with
[] -> [[]]
| x::xs ->
List.flatten
(List.map (fun l -> (insertions x l)) (permutations xs))
in
List.flatten
(List.map (permutations) (sublists l))
(*
Binary operators to use.
*)
type operator = Add | Sub | Mul | Div
(*
Applying operator o to operands m and n
yields a natural number.
*)
let valid o m n = match o with
Add -> true
| Sub -> (m > n)
| Mul -> true
| Div -> (m mod n) = 0
(*
Applying operator o to operands m and n
yields a natural number. (Optimized.)
*)
let valid' o m n = match o with
Add -> (m <= n)
| Sub -> (m > n)
| Mul -> (m <> 1) && (n <> 1) && (m <= n)
| Div -> (n <> 1) && (m mod n) = 0
(*
Result of applying operator o to operands
m and n.
*)
let apply o m n = match o with
Add -> m + n
| Sub -> m - n
| Mul -> m * n
| Div -> m / n
(*
An expression is a single natural number or
an application of an operator to two operands.
*)
type expression =
Val of int
| App of operator * expression * expression
(*
Return a list of all natural numbers used in
expression e.
*)
let rec values e = match e with
Val i -> [i]
| App(_, e1, e2) -> (values e1) @ (values e2)
(*
Result of evaluating expression e.
Quote from Hutton:
"Failure within eval is handled by returning
a list of results, with the convention that a
singleton list denotes success, and the empty
list denotes failure."
Maybe it is more elegant to handle this with
exceptions.
*)
let rec eval e = match e with
Val i ->
if i > 0 then [i] else []
| App(o, e1, e2) ->
match (eval e1, eval e2) with
([e1'], [e2']) ->
if valid' o e1' e2' then
[apply o e1' e2']
else
[]
| _ -> []
(*
Expression e is a solution to the problem with
numbers and number.
*)
let solution e numbers number =
(List.mem (values e) (subbags numbers))
&& ((eval e) = [number])
(*
Return a list of all pairs (l,r) where l and r
appended yield the list l and l and r are not
empty.
*)
let ne_split l =
(*
Return a list of all pairs (l,r) where l and r
appended yield the list l.
*)
let rec split l = match l with
[] -> [([], [])]
| x::xs ->
([], l) ::
(List.map (fun (l, r) -> (x::l, r)) (split xs))
in
(*
Lists l and r are both not empty.
*)
let no_empty (l, r) = not(l=[] || r=[])
in
List.filter no_empty (split l)
(*
Generate a list of all possible expressions
over the natural numbers in list l.
*)
let rec expressions l = match l with
[] -> []
| [n] -> [Val n]
| _ ->
(*
A list of applying each operator to l
and r.
*)
let aps (l, r) = [App(Add, l, r);
App(Sub, l, r);
App(Mul, l, r);
App(Div, l, r)]
in
(*
Given two lists L1 and L2, return a list of all
pairs (l,r) where l is an element of L1 and r
is an element of L2.
*)
let rec combine l1 l2 = match l1 with
[] -> []
| x::xs ->
(List.map (fun e -> (x,e)) l2)
@ (combine xs l2)
in
(*
A list of pairs (l1,l2) where l1 is a list
of all expressions over a non-empty left
sublist of l and l2 a list of all
expressions over the remaining non-empty
right sublist of l.
*)
let pairs =
let make_exprs (l, r) = (expressions l), (expressions r) in
List.map make_exprs (ne_split l)
in
(*
A nested list of pairs (l,r) where (l,r)
is a combination of two elements from the
list pairs (defined above).
*)
let pairs' = List.map (fun (l, r) -> combine l r) pairs
in
List.flatten (List.map aps (List.flatten pairs'))
(*
A list of expressions using only values from
numbers and whose evaluated value is number.
*)
let solutions numbers number =
List.filter (fun e -> (eval e) = [number])
(List.flatten (List.map expressions (subbags numbers)))
(************************************************
Below is the first optimization as suggested
by Hutton.
************************************************)
(*
This optimization is a real must-have in
languages with strict evaluation like OCaml
(and unlike Haskell).
Invalid expressions are now filtered out
earlier, so there will not be a lot of time
spent on evaluating them.
In a lazy language, a lot of this work will
be suspended (but the optimization is still
worth quite a bit).
*)
(*
A result is an expression and its evaluation.
*)
type result = (expression * int)
(*
Return list of possible results for list of
natural numbers l.
Excuse me for the (especially in this function)
unreadable source code due to the flatten's and
map's. I tried to translate the list
comprehensions of Hutton as directly as
possible. I admit this is ugly.
*)
let rec results l = match l with
[] -> []
| [n] -> if n > 0 then [(Val n, n)] else []
| _ ->
(*
List of possible results of applying an
operator to l and r.
*)
let combine (l, x) (r, y) =
let ops = [Add; Sub; Mul; Div] in
List.map
(fun o -> (App(o, l, r), apply o x y))
(List.filter (fun o -> valid' o x y) ops)
in
(*
List of all possible results of combining
an element of ls with an element of rs.
*)
let combined_results (ls, rs) =
List.flatten
(List.map
(fun lx ->
List.flatten
(List.map (fun ry -> combine lx ry) (results rs)))
(results ls))
in
List.flatten
(List.map
combined_results
(ne_split l))
(*
A list of expressions over numbers that yield
number on evaluation.
*)
let solutions' numbers number =
(*
A list of all 'good' expressions over values
of list l.
*)
let exprs l =
let check (e, m) = (m = number) in
List.map (fun (e, m) -> e) (List.filter check (results l))
in
List.flatten (List.map exprs (subbags numbers))
(************************************************
What is left is a way to work with al this.
************************************************)
(*
String representation of expression e.
*)
let rec expression_to_string e =
(*
String representation of operator o.
*)
let operator_to_string o = match o with
Add -> "+"
| Sub -> "-"
| Mul -> "*"
| Div -> "/"
in
match e with
Val n -> string_of_int n
| App(o, l, r) ->
begin
match l with
Val _ -> expression_to_string l
| _ -> "(" ^ (expression_to_string l) ^ ")"
end
^ " " ^ (operator_to_string o) ^ " " ^
begin
match r with
Val _ -> expression_to_string r
| _ -> "(" ^ (expression_to_string r) ^ ")"
end
(*
Reads a number and a list of numbers from
stdin and prints all solutions to that
instance of the countdown problem.
*)
let countdown () =
print_string "The Countdown Problem\n";
print_string "Please enter a positive natural number: ";
let number = try read_int () with
End_of_file ->
print_string "You quit too soon.\n";
exit 1
| Failure _ ->
print_string "That's not a number.\n";
exit 1
in
let numbers = ref [] in
let rec loop () =
match read_int () with
0 -> ()
| n ->
numbers := n :: !numbers;
try loop () with
End_of_file -> ()
in
print_string "Please enter some positive natural numbers ";
print_string "(each on its own line) ending\n";
print_string "with 0 or end-of-file:\n";
begin
try loop () with
End_of_file ->
print_string "You quit too soon.\n";
exit 1
| Failure _ ->
print_string "That's not a non-empty list of numbers.\n";
exit 1
end;
print_string "I can make ";
print_int number;
print_string " from these numbers in the following ways:\n";
flush stdout;
let print e =
print_string (expression_to_string e);
print_newline ()
in
let s = solutions' !numbers number in
List.iter print s;
if List.length s > 1 then
begin
print_string "This were ";
print_int (List.length s);
print_string " solutions.\n"
end
else if List.length s = 1 then
print_string "This was the only solution.\n"
else
print_string "Sorry, there are no solutions to this problem.\n"
(*
Start main program.
*)
let _ = countdown ()