diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/variables.bdl | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/examples/variables.bdl b/examples/variables.bdl index 7b343d1..a4dea0a 100644 --- a/examples/variables.bdl +++ b/examples/variables.bdl | |||
@@ -2,13 +2,35 @@ | |||
2 | ;; Variable declarations and updates | 2 | ;; Variable declarations and updates |
3 | ;; | 3 | ;; |
4 | 4 | ||
5 | (print "(def a 20)") (def a 20) (newline) | 5 | (print "(def a 20)") |
6 | (print "((lambda (a b) (+ 10 a b)) 1 2) -> ") ((lambda (a b) (+ 10 a b)) 1 2) | 6 | (def a 20) |
7 | (print "((lambda (a b) (+ 10 a b)) a 3) -> ") ((lambda (a b) (+ 10 a b)) a 3) | 7 | (newline) |
8 | (print "(def myfun (lambda (a b) (+ a b))) (myfun 6 9) -> ") (def myfun (lambda (a b) (+ a b))) (myfun 6 9) | 8 | |
9 | (print "(fun myfun (a b) (+ a b)) (myfun 6 9) -> ") (fun myfun (a b) (+ a b)) (myfun 6 9) | 9 | (print "((lambda (a b) (+ 10 a b)) 1 2) -> ") |
10 | (print "(+ 1 (myfun 10 (myfun a a)) 30) -> ") (+ 1 (myfun 10 (myfun a a)) 30) | 10 | (display ((lambda (a b) (+ 10 a b)) 1 2)) |
11 | (print "(myfun 10 (myfun 5 0)) -> ") (myfun 10 (myfun 5 0)) | 11 | (newline) |
12 | |||
13 | (print "((lambda (a b) (+ 10 a b)) a 3) -> ") | ||
14 | (display ((lambda (a b) (+ 10 a b)) a 3)) | ||
15 | (newline) | ||
16 | |||
17 | (print "(def myfun (lambda (a b) (+ a b))) (myfun 6 9) -> ") | ||
18 | (def myfun (lambda (a b) (+ a b))) | ||
19 | (display (myfun 6 9)) | ||
20 | (newline) | ||
21 | |||
22 | (print "(fun myfun (a b) (+ a b)) (myfun 6 9) -> ") | ||
23 | (fun myfun (a b) (+ a b)) | ||
24 | (display (myfun 6 9)) | ||
25 | (newline) | ||
26 | |||
27 | (print "(+ 1 (myfun 10 (myfun a a)) 30) -> ") | ||
28 | (display (+ 1 (myfun 10 (myfun a a)) 30)) | ||
29 | (newline) | ||
30 | |||
31 | (print "(myfun 10 (myfun 5 0)) -> ") | ||
32 | (display (myfun 10 (myfun 5 0))) | ||
33 | (newline) | ||
12 | 34 | ||
13 | ;; Closures. | 35 | ;; Closures. |
14 | (print "(fun make-counter () (def value 0) (def counter (lambda () (set! value (+ value 1)) value)) counter)") | 36 | (print "(fun make-counter () (def value 0) (def counter (lambda () (set! value (+ value 1)) value)) counter)") |
@@ -22,14 +44,14 @@ | |||
22 | 44 | ||
23 | (print "(def counter-a (make-counter))") (def counter-a (make-counter)) (newline) | 45 | (print "(def counter-a (make-counter))") (def counter-a (make-counter)) (newline) |
24 | (print "(def counter-b (make-counter))") (def counter-b (make-counter)) (newline) | 46 | (print "(def counter-b (make-counter))") (def counter-b (make-counter)) (newline) |
25 | (print "(counter-a) -> ") (counter-a) | 47 | (print "(counter-a) -> ") (display (counter-a))(newline) |
26 | (print "(counter-b) -> ") (counter-b) | 48 | (print "(counter-b) -> ") (display (counter-b))(newline) |
27 | (print "(counter-a) -> ") (counter-a) | 49 | (print "(counter-a) -> ") (display (counter-a))(newline) |
28 | (print "(counter-a) -> ") (counter-a) | 50 | (print "(counter-a) -> ") (display (counter-a))(newline) |
29 | (print "(counter-a) -> ") (counter-a) | 51 | (print "(counter-a) -> ") (display (counter-a))(newline) |
30 | (print "(counter-b) -> ") (counter-b) | 52 | (print "(counter-b) -> ") (display (counter-b))(newline) |
31 | (print "(counter-b) -> ") (counter-b) | 53 | (print "(counter-b) -> ") (display (counter-b))(newline) |
32 | (print "(counter-b) -> ") (counter-b) | 54 | (print "(counter-b) -> ") (display (counter-b))(newline) |
33 | 55 | ||
34 | ;; Fibonacci. | 56 | ;; Fibonacci. |
35 | (print "(fun fib (n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))") (newline) | 57 | (print "(fun fib (n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))") (newline) |
@@ -38,7 +60,8 @@ | |||
38 | 1 | 60 | 1 |
39 | (+ (fib (- n 1)) (fib (- n 2))))) | 61 | (+ (fib (- n 1)) (fib (- n 2))))) |
40 | 62 | ||
41 | (print "(fib 15) -> ")(fib 15) | 63 | (print "(fib 15) -> ") |
64 | (display (fib 15))(newline) | ||
42 | 65 | ||
43 | ;; Lambda capture. | 66 | ;; Lambda capture. |
44 | (print "(fun b () (display a) (print \" --- \") (def a 42) (display a) (newline))") (newline) | 67 | (print "(fun b () (display a) (print \" --- \") (def a 42) (display a) (newline))") (newline) |
@@ -52,11 +75,10 @@ | |||
52 | (print "(b) -> ") (b) | 75 | (print "(b) -> ") (b) |
53 | (print "(b) -> ") (b) | 76 | (print "(b) -> ") (b) |
54 | 77 | ||
55 | |||
56 | ;; Infinite loop. (For teseting purposes) | 78 | ;; Infinite loop. (For teseting purposes) |
57 | ; (def test (lambda (n) | 79 | ; (def test (lambda (n) |
58 | ; (print "ITER\n") | 80 | ; (print "ITER\n") |
59 | ; (if (<= n 2) | 81 | ; (if (<= n 2) |
60 | ; 'ok | 82 | ; 1 |
61 | ; (test (+ n 1))))) | 83 | ; (test (+ n 1))))) |
62 | ; (test 3) | 84 | ; (test 3) |