-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.scm
63 lines (56 loc) · 1.53 KB
/
misc.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
(define dbg-enabled #f)
(define (debug x) (if dbg-enabled (display x) 'no-debugging))
(define (debug-newline) (if dbg-enabled (newline) 'no-debugging))
(define (emit str)
(if (not dbg-enabled)
(begin
(display str)
(newline))
'debugging))
(define (take lst n)
(cond
((equal? n 0) '())
((null? lst) '())
(else (cons (car lst) (take (cdr lst) (sub1 n))))))
(define (drop lst n)
(cond
((equal? n 0) lst)
((null? lst) '())
(else (drop (cdr lst) (sub1 n)))))
(define (indexed-map_ f l i)
(if (null? l)
'()
(cons (f i (car l))
(indexed-map_ f (cdr l) (add1 i)))))
(define (indexed-map f l)
(indexed-map_ f l 0))
(define (element? e x)
(cond
((null? x) #f)
((eq? e (car x)) #t)
(else (element? e (cdr x)))))
(define (set-union x y)
(cond
((and (null? x) (null? y)) '())
((null? x) y)
((null? y) x)
((element? (car x) y) (set-union (cdr x) y))
(else (set-union (cdr x) (cons (car x) y)))))
(define (set-union-many x)
(cond
((null? x) '())
((equal? (length x) 1) (car x))
(else (set-union (car x) (set-union-many (cdr x))))))
(define (set-substract x y)
(filter (lambda (z) (not (element? z y))) x))
(define (extend-env var val env)
(debug "EXTEND-ENV var = ") (debug var) (debug-newline)
(debug "EXTEND-ENV val = ") (debug val) (debug-newline)
(cons (cons var val) env))
(define (extend-env-many vars vals env)
(if (null? vars)
env
(extend-env-many
(cdr vars)
(cdr vals)
(extend-env (car vars) (car vals) env))))