|
| 1 | +# ref: http://lisperator.net/pltut/eval1/play |
| 2 | + |
| 3 | +print_range = λ(a, b) if a <= b { |
| 4 | + println(a); |
| 5 | + if a + 1 <= b { |
| 6 | + print_range(a + 1, b); |
| 7 | + } |
| 8 | + else |
| 9 | + println(""); |
| 10 | + }; |
| 11 | +print_range(1, 10); |
| 12 | + |
| 13 | +cons = λ(a, b) λ(f) f(a, b); |
| 14 | +car = λ(cell) cell(λ(a, b) a); |
| 15 | +cdr = λ(cell) cell(λ(a, b) b); |
| 16 | +NIL = λ(f) f(NIL, NIL); |
| 17 | + |
| 18 | +x = cons(1, cons(2, cons(3, cons(4, cons(5, NIL))))); |
| 19 | +println(car(x)); # 1 |
| 20 | +println(car(cdr(x))); # 2 |
| 21 | +println(car(cdr(cdr(x)))); # 3 |
| 22 | +println(car(cdr(cdr(cdr(x))))); # 4 |
| 23 | +println(car(cdr(cdr(cdr(cdr(x)))))); # 5 |
| 24 | + |
| 25 | + |
| 26 | +# code = fs-readFileSync("./demo"); |
| 27 | +# println(code); |
| 28 | +# println(os-arch()); |
| 29 | + |
| 30 | +add = lambda(a, b){ |
| 31 | + 1; |
| 32 | +# return the last expression |
| 33 | + a + b |
| 34 | +}; |
| 35 | +println(add(100, 133)); |
| 36 | + |
| 37 | +foreach = λ(list, f) |
| 38 | + if list != NIL { |
| 39 | + f(car(list)); |
| 40 | + foreach(cdr(list), f); |
| 41 | + }; |
| 42 | +foreach(x, println); |
| 43 | + |
| 44 | +range = λ(a, b) |
| 45 | + if a <= b then cons(a, range(a + 1, b)) |
| 46 | + else NIL; |
| 47 | + |
| 48 | +# print the squares of 1..8 |
| 49 | +foreach(range(1, 8), λ(x) println(x * x)); |
| 50 | + |
| 51 | + |
| 52 | +cons = λ(x, y) |
| 53 | + λ(a, i, v) |
| 54 | + if a == "get" |
| 55 | + then if i == 0 then x else y |
| 56 | + else if i == 0 then x = v else y = v; |
| 57 | + |
| 58 | +car = λ(cell) cell("get", 0); |
| 59 | +cdr = λ(cell) cell("get", 1); |
| 60 | +set-car! = λ(cell, val) cell("set", 0, val); |
| 61 | +set-cdr! = λ(cell, val) cell("set", 1, val); |
| 62 | + |
| 63 | +# NIL can be a real cons this time |
| 64 | +NIL = cons(0, 0); |
| 65 | +set-car!(NIL, NIL); |
| 66 | +set-cdr!(NIL, NIL); |
| 67 | + |
| 68 | +## test: |
| 69 | +x = cons(1, 2); |
| 70 | +println(car(x)); |
| 71 | +println(cdr(x)); |
| 72 | +set-car!(x, 10); |
| 73 | +set-cdr!(x, 20); |
| 74 | +println(car(x)); |
| 75 | +println(cdr(x)); |
| 76 | + |
| 77 | + |
| 78 | +## IIFE |
| 79 | +println(let loop (n = 100) |
| 80 | + if n > 0 then n + loop(n - 1) |
| 81 | + else 0); |
| 82 | + |
| 83 | +let (x = 2, y = x + 1, z = x + y) |
| 84 | + println(x + y + z); |
| 85 | + |
| 86 | +# errors out, the vars are bound to the let body |
| 87 | +# print(x + y + z); |
| 88 | + |
| 89 | +let (x = 10) { |
| 90 | + let (x = x * 2, y = x * x) { |
| 91 | + println(x); ## 20 |
| 92 | + println(y); ## 400 |
| 93 | + }; |
| 94 | + println(x); ## 10 |
| 95 | +}; |
| 96 | + |
| 97 | +fib = λ(n) if n < 2 then n else fib(n - 1) + fib(n - 2); |
| 98 | +println(fib(22)); |
| 99 | + |
| 100 | +copyFile = lambda(source, dest){ |
| 101 | + writeFile(dest, readFile(source)) |
| 102 | +}; |
| 103 | +copyFile("demo", "demo2"); |
| 104 | + |
| 105 | + |
| 106 | +println(1 + twice(2, 3)); |
| 107 | + |
0 commit comments