aboutsummaryrefslogtreecommitdiff
path: root/files/test-lambda.l
blob: 31364523e074107866a65560769fb2027f2ccb23 (plain)
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
(define make-add (lambda (a) (lambda (b) (+ a b))))
(define add4 (make-add 4))
(add4 5)

`(a b ,((lambda (a) `(test . ,a)) 69) c d)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define fib (lambda (c) (_fib 0 1 0 c)))
(define _fib (lambda (a b n c)
                  (if (= n c)
                      b
                      (_fib b (+ a b) (+ n 1) c))))





(define do (lambda (c f) (_do f '() 0 c)))

(define _do (lambda (f r n c)
                 (if (= n c) r
                     (_do f (cons (f n) r) (+ n 1) c))))

;; comment
;; another comment
;; fib(13)
;(do 10 (lambda (n) (display (fib n)))) ; comment

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define +list (lambda (l) (_+list l 0)))
(define _+list (lambda (l acc)
                 (if (nil? l)
                     acc
                     (_+list (cdr l) (+ acc (car l))))))
(+list '(10 12))

(define * (lambda (a n) (+list (do n (lambda (n) a)))))

(* 9 7)

(define reverse (lambda (l) (_reverse l '())))
(define _reverse (lambda (l n)
                   (if (nil? l) n
                       (_reverse (cdr l) (cons (car l) n)))))

;; (+list (do 100 (lambda (n) (fib n))))
(reverse (do 100 (lambda (n) (fib n))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; don't know about a good example
(defmacro m (a) `(+ ,a 57))
(m (+ 1 2))

(define map (lambda (fn list)
              (if (nil? list)
                  list
                  (cons (fn (car list)) (map fn (cdr list))))))

(map (lambda (n) (+ n 1)) '(1 2 3))

(define foldl (lambda (fn acc l)
                (if (nil? l) acc
                    (foldl fn (fn (car l) acc) (cdr l)))))
(define foldr (lambda (fn acc l)
                (if (nil? l) acc
                    (fn (car l) (foldr fn acc (cdr l))))))


(define cdar (lambda (l) (car (cdr l))))

(defmacro let (vars body)
  `((lambda ,(map car vars) ,body) . ,(map cdar vars)))

(defmacro let* (vars body)
  (foldr (lambda (var rest) `(let (,var) ,rest)) body vars))

(defmacro cond (vars)
  (foldr (lambda (clause rest) `(if ,(car clause) ,(cdar clause) ,rest)) 1 vars))

(let ((a 9)
      (b 13))
  (+ a b))

(let* ((a 8)
       (b (- a 3))
       (c (+ a b)))
  (- c 3))

(cond (((= 1 2) 3)
       ((= 1 (+ 1 1)) (+ 6 2))
       (1 (quote test))))

(defmacro begin (vars)
  (foldr (lambda (var rest) `((lambda (__) ,rest) ,var)) '__ vars))

(begin
 ((define inc (lambda (a) (+ a 1)))
  (inc 1)))

(define inc (lambda (a) (+ a 1)))

(begin
 ((+ 0 1)
  (define a inc)))
  ;; (a 1)))