1: doctype "calculator-lambda-0_2_1.g.etl";
2:
3: /// The special marker value used to stop computation in sum function
4: let STOP = \x => x;
5: /// Sum arguments up to stop marker like {@code sum(0,1,2,3,STOP)}
6: ///
7: /// @return the function that accumulates results.
8: let sum = {?;
9: var sum = 0;
10: to acc(x) => if( x == STOP) sum else {sum = sum + x; acc;};
11: };
12: /// Greatest common divisor
13: ///
14: /// @param a the positive integer value
15: /// @param b the positive integer value
16: /// @return the greatest common divisor
17: let gcd = {? var q'a', var b;
18: assertInt(a,b);
19: assert a > 0 && b > 0;
20: if(a < b) {
21: let t = a;
22: a = Q'b';
23: b = t;
24: };
25: while(b != 0) {
26: let r = a % b;
27: a = b;
28: b = r;
29: };
30: a;
31: };
32:
33:
34: assert 11 !*= {?; 12;};
35: assert (\> 12) =*= (& 12);
36: assert 12 == * (\()=> \> & max(10)(12));
37: assert 12 == * (\()=> \> #q'&' {?;1+10+1;});
38: assert 12 == #abs 1-13;
39: assert 12 == sum(0,1,2,3,3,2,1,0,STOP);
40: assert 12 == factorial(4) / 2;
41: assert 12 == 420 #gcd 140 + 4;
42: assert 12 == {
43: var s = 0;
44: 0 #upto 4 {?i;
45: s = s + i;
46: };
47: s * 2;
48: };
49: assert 12 == {
50: var s = 0;
51: var n = 0;
52: for(14, 9.9, -0.25) {?i;
53: s = s + i;
54: n = n + 1;
55: };
56: s / n;
57: };
58: assert 1 == #sign 12;
59: assert -1 == #sign -MIN_NUMBER;
60: assert 0 == sign(12-12);
61: assert 12 ==~ {?n; sign(n) == 1;};
62: assert ! 12 ==~ (\n => sign(n) == -1);