@@ -10,6 +10,7 @@ next: true
1010From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
1111
1212```
13+@Memoized
1314def i(def a) {
1415 a.class in [
1516 byte, Byte,
@@ -21,28 +22,40 @@ def i(def a) {
2122 ] && a >= 0
2223}
2324
25+@Memoized
2426def n(def a) {
27+ def r
28+ n(a, { r = it })
29+ r
30+}
31+
32+@TailRecursive
33+def n(def a, def r) {
2534 if (a in List) {
2635 if (a.size() == 1) {
27− n(a[0])
28− } else if (a.size() == 2) {
29− [n(a[0]), n(a[1])]
30− } else if (a.size() > 2) {
31− [n(a[0]), n(a.tail())]
36+ r(a[0])
37+ } else if (a.size() >= 2) {
38+ n(a[0], { t ->
39+ n(a.size() == 2 ? a[1] : a.tail(), { h ->
40+ r([t, h])
41+ })
42+ })
3243 } else {
3344 throw new IllegalStateException()
3445 }
3546 } else if (i(a)) {
36− (BigInteger) a
47+ r((BigInteger) a)
3748 } else {
3849 throw new IllegalStateException()
3950 }
4051}
4152
53+@Memoized
4254def wut(def a) {
4355 i(a) ? 1 : 0
4456}
4557
58+@Memoized
4659def lus(def a) {
4760 if (wut(a) == 0) {
4861 throw new IllegalStateException()
@@ -50,6 +63,7 @@ def lus(def a) {
5063 1 + a
5164}
5265
66+@Memoized
5367def tis(def a) {
5468 if (wut(a) == 1) {
5569 throw new IllegalStateException()
@@ -57,7 +71,15 @@ def tis(def a) {
5771 a[0] == a[1] ? 0 : 1
5872}
5973
74+@Memoized
6075def fas(def a) {
76+ def r
77+ fas(a, { r = it })
78+ r
79+}
80+
81+@TailRecursive
82+def fas(def a, def r) {
6183 if (wut(a) == 1) {
6284 throw new IllegalStateException()
6385 }
@@ -69,27 +91,34 @@ def fas(def a) {
6991 if (h == 0) {
7092 throw new IllegalStateException()
7193 } else if (h == 1) {
72− t
94+ r(t)
7395 } else {
7496 if (i(t)) {
7597 throw new IllegalStateException()
7698 }
7799 if (h == 2) {
78− t[0]
100+ r(t[0])
79101 } else if (h == 3) {
80− t[1]
102+ r(t[1])
81103 } else {
82− def x = h.intdiv(2)
83− if (h.mod(2) == 0) {
84− fas([2, fas([x, t])])
85− } else {
86− fas([3, fas([x, t])])
87− }
104+ fas([h.intdiv(2), t], { p ->
105+ fas([2 + h.mod(2), p], { q ->
106+ r(q)
107+ })
108+ })
88109 }
89110 }
90111}
91112
113+@Memoized
92114def tar(def a) {
115+ def r
116+ tar(a, { r = it})
117+ r
118+}
119+
120+@TailRecursive
121+def tar(def a, def r) {
93122 if (wut(a) == 1) {
94123 throw new IllegalStateException()
95124 }
@@ -101,18 +130,28 @@ def tar(def a) {
101130 def o = f[0]
102131 def v = f[1]
103132 if (wut(o) == 0) {
104− [tar([s, o]), tar([s, v])]
133+ tar([s, o], { p ->
134+ tar([s, v], { q ->
135+ r([p, q])
136+ })
137+ })
105138 } else {
106139 if (o == 0) {
107− fas([v, s])
140+ r(fas([v, s]))
108141 } else if (o == 1) {
109− v
142+ r(v)
110143 } else if (o == 3) {
111− wut(tar([s, v]))
144+ tar([s, v], {
145+ r(wut(it))
146+ })
112147 } else if (o == 4) {
113− lus(tar([s, v]))
148+ tar([s, v], {
149+ r(lus(it))
150+ })
114151 } else if (o == 5) {
115− tis(tar([s, v]))
152+ tar([s, v], {
153+ r(tis(it))
154+ })
116155 } else {
117156 if (wut(v) == 1) {
118157 throw new IllegalStateException()
@@ -120,29 +159,43 @@ def tar(def a) {
120159 def x = v[0]
121160 def y = v[1]
122161 if (o == 2) {
123− tar([tar([s, x]), tar([s, y])])
162+ tar([s, x], { p ->
163+ tar([s, y], { q ->
164+ tar([p, q], {
165+ r(it)
166+ })
167+ })
168+ })
124169 } else if (o == 7) {
125− tar(n([s, 2, x, 1, y]))
170+ tar(n([s, 2, x, 1, y]), {
171+ r(it)
172+ })
126173 } else if (o == 8) {
127− tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]))
174+ tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]), {
175+ r(it)
176+ })
128177 } else if (o == 9) {
129− tar(n([s, 7, y, 2, [0, 1], 0, x]))
178+ tar(n([s, 7, y, 2, [0, 1], 0, x]), {
179+ r(it)
180+ })
130181 } else if (o == 10) {
131182 if (wut(x) == 1) {
132− tar([s, y])
183+ tar([s, y], {
184+ r(it)
185+ })
133186 } else {
134− def r = x[0]
135− def t = x[1]
136− tar(n([s, 8, t, 7, [0, 3], y]))
187+ tar(n([s, 8, x[1], 7, [0, 3], y]), {
188+ r(it)
189+ })
137190 }
138191 } else {
139192 if (wut(y) == 1) {
140193 throw new IllegalStateException()
141194 }
142− def p = y[0]
143− def q = y[1]
144195 if (o == 6) {
145− tar(n([s, 2, [0, 1], 2, [1, p, q], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]))
196+ tar(n([s, 2, [0, 1], 2, [1, y[0], y[1]], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]), {
197+ r(it)
198+ })
146199 } else {
147200 throw new IllegalStateException()
148201 }