Skip to content

Commit 5ea25f0

Browse files
committed
c4 23
1 parent 64a6521 commit 5ea25f0

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/eval/c4_23.scm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; I've implemented one analyze-sequence in eval1.scm,
2+
; the text has a version of analyze-sequence(analyze-sequence-text in eval1.scm)
3+
; and analyze-sequence-alyssa has a version of analyze-sequence(analyze-sequence-alyssa).
4+
; Of those, analyze-sequence-text and my version are better. Since they try to expand all the sequence
5+
; during the analysis stage(try to add a print func in loop).

src/eval/eval1.scm

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,46 @@
6969
(lambda (env) (make-procedure params body-proc env))))
7070

7171
(define (analyze-sequence exp)
72+
(define (combine proc1 proc2)
73+
(lambda (env)
74+
(proc1 env)
75+
(proc2 env)))
7276
(define (combine-seq-proc seq-proc)
77+
; (println "combine-seq-proc")
7378
(if (null? (cdr seq-proc))
7479
(car seq-proc)
75-
(lambda (env)
76-
((car seq-proc) env)
77-
((combine-seq-proc (cdr seq-proc)) env))))
80+
(combine (car seq-proc)
81+
(combine-seq-proc (cdr seq-proc)))))
7882
(let ((seq-proc (map analyze exp)))
7983
(if (null? seq-proc)
8084
(error 'analyze-sequence "no expression in code block")
8185
(combine-seq-proc seq-proc))))
8286

87+
(define (analyze-sequence-text exps)
88+
(define (sequentially proc1 proc2)
89+
(lambda (env) (proc1 env) (proc2 env)))
90+
(define (loop first-proc rest-procs)
91+
; (println "loop")
92+
(if (null? rest-procs)
93+
first-proc
94+
(loop (sequentially first-proc (car rest-procs))
95+
(cdr rest-procs))))
96+
(let ((procs (map analyze exps)))
97+
(if (null? procs)
98+
(error "Empty sequence -- ANALYZE"))
99+
(loop (car procs) (cdr procs))))
100+
101+
(define (analyze-sequence-alyssa exps)
102+
(define (execute-sequence procs env)
103+
(cond ((null? (cdr procs))
104+
((car procs) env))
105+
(else ((car procs) env)
106+
(execute-sequence (cdr procs) env))))
107+
(let ((procs (map analyze exps)))
108+
(if (null? procs)
109+
(error "Empty sequence -- ANALYZE"))
110+
(lambda (env) (execute-sequence procs env))))
111+
83112
(define (analyze-application exp)
84113
(let ((fproc (analyze (operator exp)))
85114
(aprocs (map analyze (operand exp))))
@@ -125,3 +154,11 @@
125154
(set! a (* a b))
126155
(+ a b))
127156
global-env))
157+
158+
(define seq1 '((set! a b)
159+
(cons a b)
160+
(cons a b)
161+
(cons a b)
162+
(cons a b)))
163+
164+
(define seq2 '((cons a b)))

0 commit comments

Comments
 (0)