-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.scm
125 lines (100 loc) · 3.06 KB
/
primitives.scm
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
;; primitives.scm
(define (foldl oper init lis)
(if (null? lis)
init
(foldl oper (oper init (car lis)) (cdr lis))))
(define (foldr oper init lis)
(if (null? lis)
init
(oper (car lis) (foldr oper init (cdr lis)))))
(define (v-map proc lis)
(foldr (lambda (first result) (cons (proc first) result)) '() lis))
(define (v-filter pred lis)
(define (filter-helper first result)
(if (pred first)
(cons first result)
result))
(foldr filter-helper '() lis))
(define (v-reverse lis)
(foldl (lambda (init first) (cons first init)) '() lis))
(define (v-length lis)
(foldl (lambda (init first) (+ init 1)) 0 lis))
(define (v-append list-a list-b)
(foldr cons list-b list-a))
(define (v-list-ref lis index)
(if (and (< index (v-length lis)) (>= index 0))
(if (= index 0)
(car lis)
(v-list-ref (cdr lis) (- index 1)))
(error "Index out of range" index)))
(define (v-for-each proc lis)
(if (null? lis)
'done
(begin (proc (car lis))
(v-for-each proc (cdr lis)))))
(define (v-remove ele lis)
(define (remove-helper first init)
(if (equal? first ele)
init
(cons first init)))
(foldr remove-helper '() lis))
(define (v-remove-first ele lis)
(if (not (null? lis))
(if (equal? (car lis) ele)
(cdr lis)
(cons (car lis) (v-remove-first ele (cdr lis))))
(error "Not in list ele:" ele)))
(define (v-and exps)
(cond ((false? (car exps)) 'false)
((null? exps) 'true)
(else (v-and (cdr exps)))))
(define (v-or exps)
(cond ((null? exps) 'false)
((not (false? (car exps))) 'true)
(else (v-or (cdr exps)))))
(define (range num)
(define (range-helper start)
(if (= start (- num 1))
(cons start '())
(cons start (range-helper (+ start 1)))))
(range-helper 0))
(define (my-abs num)
(if (< num 0)
(* -1 num)
num))
(define (v-even? num) (= (remainder num 2) 0))
(define (v-odd? num) (not (v-even? num)))
(define (println data)
(newline)
(display data)
(newline)
'done)
;TODO: Rename keywords to make fancier names
;; 1. cons -> pair
;; 2. car -> head
;; 3. cdr -> tail
(define (primitive-proc? proc) (oper=? proc 'primitive))
(define primitive-procs
(list (list 'foldl foldl)
(list 'foldr foldr)
(list 'map v-map)
(list 'filter v-filter)
(list 'reverse v-reverse)
(list 'length v-length)
(list 'append v-append)
(list 'list-ref v-list-ref)
(list 'for-each v-for-each)
(list 'remove 'v-remove)
(list 'remove-first 'v-remove-first)
(list 'head car)
(list 'tail cdr)
(list 'pair cons)
(list 'null? null?)
(list '+ +)
(list '* *)))
(define (primitive-proc-names) (map car primitive-procs))
(define (primitive-proc-vals)
(map (lambda (proc) (cons 'primitive (cadr proc))) primitive-procs))
(define (name->primitive proc) (car (cdr proc)))
(define (apply-primitive-proc proc args)
(apply-in-scheme (name->primitive proc) args))