-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlocal-form.scm
55 lines (48 loc) · 1013 Bytes
/
local-form.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
; From:
;; (begin
;; (foo)
;; (define a (lambda () 1))
;; (define b (lambda () 2))
;; (a))
; To:
;; ((lambda (a b)
;; (foo)
;; (set! a (lambda () 1))
;; (set! b (lambda () 2))
;; (a))
;; #f #f)
(define local-form
(lambda (exp)
(if (not (begin-exp? exp))
exp
(let ([vars (defined-vars (cdr exp))])
`((lambda ,vars
,@(replace-define (cdr exp)))
,@(map (lambda (v) #f) vars))))))
(define defined-vars
(lambda (exps)
(fold
(lambda (exp vars)
(if (define-exp? exp)
(cons (cadr exp) vars)
vars))
'()
exps)))
(define replace-define
(lambda (exps)
(map
(lambda (exp)
(if (define-exp? exp)
`(set! ,@(cdr exp))
exp))
exps)))
(define exp?
(lambda (exp sym)
(and (pair? exp)
(eq? (car exp) sym))))
(define begin-exp?
(lambda (exp)
(exp? exp 'begin)))
(define define-exp?
(lambda (exp)
(exp? exp 'define)))