-
Notifications
You must be signed in to change notification settings - Fork 0
/
barrels.fn
66 lines (60 loc) · 1.65 KB
/
barrels.fn
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
let
fn require(condition) {
condition or back
}
fn member {
(_, []) { false }
(x, x @ _) { true }
(x, _ @ t) { member(x, t) }
}
fn one_of {
([]) { back }
(h @ t) {
h then one_of(t)
}
}
fn exclude {
(items, []) { [] }
(items, h @ t) {
if (member(h, items)) {
exclude(items, t)
} else {
h @ exclude(items, t)
}
}
}
fn some_of {
([]) { back }
(h @ t) { [h] then some_of(t) then h @ some_of(t) }
}
fn sum {
([]) { 0 }
(h @ t) { h + sum(t) }
}
fn barrels_of_fun() {
// A wine merchant has six barrels of wine and beer containing:
//
// 30 gallons
// 32 gallons
// 36 gallons
// 38 gallons
// 40 gallons
// 62 gallons
//
// Five barrels are filled with wine and one with beer.
// The first customer purchases two barrels of wine.
// The second customer purchases twice as much wine as the first customer.
// Which barrel contains beer?
let
barrels = [30, 32, 36, 38, 40, 62];
beer = one_of(barrels);
wine = exclude([beer], barrels);
barrel_1 = one_of(wine);
barrel_2 = one_of(exclude([barrel_1], wine));
purchase = some_of(exclude([barrel_1, barrel_2], wine));
in
require((barrel_1 + barrel_2) * 2 == sum(purchase));
beer
}
in
print(barrels_of_fun())