aboutsummaryrefslogtreecommitdiff
path: root/files/test-lambda.l
blob: 29e1f52b98d90689628e15e278adf2c9a7d0b24d (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
(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))

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

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