-
Notifications
You must be signed in to change notification settings - Fork 1
/
startup.lisp
103 lines (82 loc) · 1.95 KB
/
startup.lisp
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
;;
;; core
;;
(define (list params...)
params)
(define (!= x y)
(not (= x y)))
(defmacro (if c then else...)
(list (quote cond) (list c then)
(cons #t else)))
(defmacro (when c then)
(list (quote cond) (list c then)))
(define (not x)
(cond (x nil)
(#t #t)))
(define (compose funcs...)
(lambda (args...)
(reduce apply args funcs)))
(defmacro (let defs body...)
(list (quote apply)
(cons (quote lambda)
(cons (map car defs) body))
(cons (quote list) (map second defs))))
(defmacro (let* defs body...)
(define (let*-iter defs)
(if defs
(let ((def (car defs)))
(list (quote apply)
(list (quote lambda) (list (car def))
(let*-iter (cdr defs)))
(cons (quote list) (cdr def))))
(cons (quote begin) body)))
(let*-iter defs))
;;
;; list operations
;;
(define (map func lst)
(when (car lst)
(cons (func (car lst))
(map func (cdr lst)))))
(define (filter func lst)
(when (car lst)
(if (func (car lst))
(cons (car lst)
(filter func (cdr lst)))
(filter func (cdr lst)))))
(define (reduce func init lst)
(if lst
(func (car lst)
(reduce func init (cdr lst)))
init))
(define (length lst)
(reduce (lambda (_ x) (+ x 1)) 0 lst))
(define (sum lst)
(reduce + 0 lst))
(define (range from to)
(let ((from (if to from 0))
(to (if to to from)))
(when (> to from)
(cons from (range (+ from 1) to)))))
(define (print-list x)
(cond (x (println (car x))
(print-list (cdr x)))))
(define (second lst)
(car (cdr lst)))
(define (reverse lst)
(define (internal-reverse sub-lst new-lst)
(if sub-lst
(internal-reverse (cdr sub-lst)
(cons (car sub-lst) new-lst))
new-lst))
(internal-reverse lst nil))
;;
;; math
;;
(define (square x)
(* x x))
(define (even x)
(= (mod x 2) 0))
(define (odd x)
(not (even x)))
(collect)