-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-1-23.scm
29 lines (28 loc) · 950 Bytes
/
ex-1-23.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
; Add if expressions
(define occurs-free?
(lambda (var exp)
(cond
((symbol? exp) (eqv? var exp))
((eqv? (car exp) 'lambda)
(and (not (eqv? (caadr exp) var))
(occurs-free? var (caddr exp))))
((eqv? (car exp) 'if)
(or (occurs-free? var (cadr exp))
(occurs-free? var (caddr exp))
(occurs-free? var (cadddr exp))))
(else (or (occurs-free? var (car exp))
(occurs-free? var (cadr exp)))))))
(define occurs-bound?
(lambda (var exp)
(cond
((symbol? exp) #f)
((eqv? (car exp) 'lambda)
(or (occurs-bound? var (caddr exp))
(and (eqv? (caadr exp) var)
(occurs-free? var (caddr exp)))))
((eqv? (car exp) 'if)
(or (occurs-free? var (cadr exp))
(occurs-free? var (caddr exp))
(occurs-free? var (cadddr exp))))
(else (or (occurs-bound? var (car exp))
(occurs-bound? var (cadr exp)))))))