-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreMoney.cpp
executable file
·158 lines (134 loc) · 3.69 KB
/
MoreMoney.cpp
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
/*
This source code is from Bartosz Milewski
https://github.com/BartoszMilewski/MoreMoney
Explains how to use the monad concept in C++
https://bartoszmilewski.com/2015/05/11/using-monads-in-c-to-solve-constraints-1-the-list-monad/
https://bartoszmilewski.com/2014/02/26/c17-i-see-a-monad-in-your-future/
https://bartoszmilewski.com/2014/10/17/c-ranges-are-pure-monadic-goodness/
*/
#include "StateList.h"
#include <vector>
using namespace std;
// select ::[a] ->[(a, [a])]
// select[] = []
// select(x:xs) = (x, xs) : [(y, x:ys) | (y, ys) <-select xs]
template<class A>
PairList<A> select(List<A> lst)
{
if (lst.isEmpty())
return PairList<A>();
A x = lst.front();
List<A> xs = lst.popped_front();
auto result = List<pair<A, State>>();
forEach(select(xs), [x, &result](pair<A, List<A>> const & p)
{
A y = p.first;
List<A> ys = p.second;
auto y_xys = make_pair(y, ys.pushed_front(x));
result = result.pushed_front(y_xys);
});
return result.pushed_front(make_pair(x, xs));
}
/*
// asNumber :: [Int] -> Int
// asNumber = foldl(\t o->t * 10 + o) 0
int asNumber(List<int> const & lst)
{
return foldl([](int acc, int d) {
return acc * 10 + d;
}, 0, lst);
}
*/
int asNumber(vector<int> const & v)
{
int acc = 0;
for (auto i : v)
{
acc = 10 * acc + i;
}
return acc;
}
StateList<pair<int, int>> testBind()
{
StateList<int> st = &select<int>;
return mbind(st, [st](int x) {
return mbind(st, [x](int y) {
return mreturn(make_pair(x, y));
});
});
}
StateList<int> testThen()
{
return mthen(mzero<int>(), []() {
cout << "Ignoring\n";
return mreturn(42);
});
}
StateList<int> testGuard()
{
StateList<int> st = &select<int>;
return mbind(st, [](int x) {
return mthen(guard(x > 2), [x]() {
return mreturn(x);
});
});
}
//s <-StateList select
//e <-StateList select
//n <-StateList select
//d <-StateList select
//m <-StateList select
//o <-StateList select
//r <-StateList select
//y <-StateList select
//guard $ s /= 0 && m /= 0
//let send = asNumber[s, e, n, d]
//more = asNumber[m, o, r, e]
//money = asNumber[m, o, n, e, y]
//guard $ send + more == money
//return (send, more, money)
StateList<tuple<int, int, int>> solve()
{
StateList<int> sel = &select<int>;
return mbind(sel, [=](int s) {
return mbind(sel, [=](int e) {
return mbind(sel, [=](int n) {
return mbind(sel, [=](int d) {
return mbind(sel, [=](int m) {
return mbind(sel, [=](int o) {
return mbind(sel, [=](int r) {
return mbind(sel, [=](int y) {
return mthen(guard(s != 0 && m != 0), [=]() {
int send = asNumber(vector<int>{s, e, n, d});
int more = asNumber(vector<int>{m, o, r, e});
int money = asNumber(vector<int>{m, o, n, e, y});
return mthen(guard(send + more == money), [=]() {
return mreturn(make_tuple(send, more, money));
});
});
});});});});});});});});
}
int main()
{
List<int> lst{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/*
StateList<int> st = &select<int>;
PairList<int> sel = runStateList(st, lst);
cout << sel;
cout << endl;
cout << evalStateList(st, lst);
cout << endl;
cout << runStateList(mreturn<int>(42), lst);
cout << endl;
cout << evalStateList(testBind(), lst);
cout << endl;
cout << evalStateList(testThen(), lst);
cout << endl;
cout << evalStateList(testGuard(), lst);
cout << endl;
cout << asNumber(lst);
cout << endl;
*/
cout << evalStateList(solve(), lst);
return 0;
}