Skip to content

Commit cb82c0c

Browse files
committed
stuck at Q19
1 parent 730884f commit cb82c0c

36 files changed

+120
-81
lines changed

Project/scheme/.ok_history

408 Bytes
Binary file not shown.

Project/scheme/.ok_messages

48.4 KB
Binary file not shown.

Project/scheme/.ok_storage.bak

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@
1010
'Problem 10-correct', (4608, 70)
1111
'Problem 11-correct', (5120, 70)
1212
'Problem 12-correct', (5632, 70)
13+
'tests.scm-correct', (6144, 70)
14+
'Problem 13-correct', (6656, 70)
15+
'Problem 14-correct', (7168, 70)
16+
'Problem 15-correct', (7680, 70)
17+
'Problem 16-correct', (8192, 70)
18+
'Problem 17-correct', (8704, 70)
19+
'Problem 18-correct', (9216, 70)

Project/scheme/.ok_storage.dat

3.5 KB
Binary file not shown.

Project/scheme/.ok_storage.dir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@
1010
'Problem 10-correct', (4608, 70)
1111
'Problem 11-correct', (5120, 70)
1212
'Problem 12-correct', (5632, 70)
13+
'tests.scm-correct', (6144, 70)
14+
'Problem 13-correct', (6656, 70)
15+
'Problem 14-correct', (7168, 70)
16+
'Problem 15-correct', (7680, 70)
17+
'Problem 16-correct', (8192, 70)
18+
'Problem 17-correct', (8704, 70)
19+
'Problem 18-correct', (9216, 70)
767 Bytes
Binary file not shown.

Project/scheme/questions.scm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,42 @@
66
; Some utility functions that you may find useful to implement.
77

88
(define (cons-all first rests)
9-
'replace-this-line)
9+
(define (f lst) (cons first lst))
10+
(map f rests)
11+
)
1012

1113
(define (zip pairs)
12-
'replace-this-line)
14+
(list (map car pairs) (map cadr pairs))
15+
)
1316

1417
;; Problem 17
1518
;; Returns a list of two-element lists
1619
(define (enumerate s)
1720
; BEGIN PROBLEM 17
18-
'replace-this-line
21+
(define (enumerate_helper s n)
22+
(if (null? s)
23+
s
24+
(cons (list n (car s)) (enumerate_helper (cdr s) (+ n 1)))))
25+
(enumerate_helper s 0)
1926
)
2027
; END PROBLEM 17
2128

2229
;; Problem 18
2330
;; List all ways to make change for TOTAL with DENOMS
2431
(define (list-change total denoms)
2532
; BEGIN PROBLEM 18
26-
'replace-this-line
33+
(cond
34+
((null? denoms) nil)
35+
((= total 0) (cons nil nil)) ; Return (())
36+
((> (car denoms) total) (list-change total (cdr denoms)))
37+
(else
38+
(define with_car (list-change (- total (car denoms)) denoms))
39+
(define without_car (list-change total (cdr denoms)))
40+
(append (cons-all (car denoms) with_car) without_car))
2741
)
2842
; END PROBLEM 18
2943

44+
)
3045
;; Problem 19
3146
;; Returns a function that checks if an expression is the special form FORM
3247
(define (check-special form)

Project/scheme/scheme.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,30 @@ def do_and_form(expressions, env):
301301
"""Evaluate a (short-circuited) and form."""
302302
# BEGIN PROBLEM 13
303303
"*** YOUR CODE HERE ***"
304+
if expressions is nil:
305+
return True
306+
else:
307+
first_expr = scheme_eval(expressions.first, env)
308+
if expressions.second is nil:
309+
return first_expr
310+
elif scheme_falsep(first_expr): # The first expression is False
311+
return False
312+
elif scheme_truep(first_expr): # The first expression is True
313+
return do_and_form(expressions.second, env)
304314
# END PROBLEM 13
305315

306316
def do_or_form(expressions, env):
307317
"""Evaluate a (short-circuited) or form."""
308318
# BEGIN PROBLEM 13
309319
"*** YOUR CODE HERE ***"
320+
if expressions is nil:
321+
return False
322+
else:
323+
first_expr = scheme_eval(expressions.first, env)
324+
if scheme_falsep(first_expr): # The first expression is False
325+
return do_or_form(expressions.second, env)
326+
else: # The first expression is True
327+
return first_expr
310328
# END PROBLEM 13
311329

312330
def do_cond_form(expressions, env):
@@ -323,6 +341,10 @@ def do_cond_form(expressions, env):
323341
if scheme_truep(test):
324342
# BEGIN PROBLEM 14
325343
"*** YOUR CODE HERE ***"
344+
if clause.second is nil: # When the true predicate does not have a corresponding result sub-expression, return the predicate value
345+
return test
346+
else: # When a result sub-expression of a cond case has multiple expressions, evaluate them all and return the value of the last expression
347+
return eval_all(clause.second, env)
326348
# END PROBLEM 14
327349
expressions = expressions.second
328350

@@ -341,6 +363,15 @@ def make_let_frame(bindings, env):
341363
raise SchemeError('bad bindings list in let form')
342364
# BEGIN PROBLEM 15
343365
"*** YOUR CODE HERE ***"
366+
formals, vals = nil, nil
367+
while bindings is not nil:
368+
check_form(bindings.first, 2, 2)
369+
val = scheme_eval(bindings.first.second.first, env) # Evaluate expressions in this env first
370+
formals = Pair(bindings.first.first, formals)
371+
check_formals(formals) # Check formals
372+
vals = Pair(val, vals)
373+
bindings = bindings.second
374+
return env.make_child_frame(formals, vals)
344375
# END PROBLEM 15
345376

346377
def do_define_macro(expressions, env):
@@ -429,6 +460,10 @@ def __init__(self, formals, body):
429460

430461
# BEGIN PROBLEM 16
431462
"*** YOUR CODE HERE ***"
463+
def make_call_frame(self, args, env):
464+
"""Make a frame that binds my formal parameters to ARGS, a Scheme list
465+
of values, for a dynamically-scoped call evaluated in environment ENV."""
466+
return env.make_child_frame(self.formals, args)
432467
# END PROBLEM 16
433468

434469
def __str__(self):
@@ -445,6 +480,8 @@ def do_mu_form(expressions, env):
445480
check_formals(formals)
446481
# BEGIN PROBLEM 16
447482
"*** YOUR CODE HERE ***"
483+
body = expressions.second
484+
return MuProcedure(formals, body)
448485
# END PROBLEM 16
449486

450487
SPECIAL_FORMS['mu'] = do_mu_form

Project/scheme/tests.scm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,18 @@
6262
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6363
;;; Move the following (exit) line to run additional tests. ;;;
6464
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65-
(exit)
65+
6666

6767

6868
;;; 1.1.2
6969

70+
(define n 1)
71+
; expect n
72+
(define n (+ n 1))
73+
; expect n
74+
n
75+
; expect 2
76+
7077
(define size 2)
7178
; expect size
7279
size
@@ -86,6 +93,11 @@ circumference
8693

8794
;;; 1.1.4
8895

96+
(define (cube x) (* x x x))
97+
; expect cube
98+
(cube 3)
99+
; expect 27
100+
89101
(define (square x) (* x x))
90102
; expect square
91103
(square 21)

Project/scheme/tests/13.py

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,20 @@
77
{
88
'code': r"""
99
scm> (and)
10-
a87b4aa619df2eba30bc86785b816612
11-
# locked
12-
# choice: True
13-
# choice: False
14-
# choice: SchemeError
10+
True
1511
scm> (and 1 False)
16-
55275a3300eb11ce47998c15403d99c6
17-
# locked
18-
# choice: 1
19-
# choice: True
20-
# choice: False
12+
False
2113
scm> (and (+ 1 1) 1)
22-
2894dd5fa65c8aa8f2b9d920d0e542e0
23-
# locked
14+
1
2415
scm> (and False 5)
25-
55275a3300eb11ce47998c15403d99c6
26-
# locked
16+
False
2717
scm> (and 4 5 (+ 3 3))
28-
7964a777121da6350b0e6ecd16129317
29-
# locked
18+
6
3019
scm> (and True False 42 (/ 1 0))
31-
55275a3300eb11ce47998c15403d99c6
32-
# locked
20+
False
3321
""",
3422
'hidden': False,
35-
'locked': True
23+
'locked': False
3624
},
3725
{
3826
'code': r"""
@@ -106,32 +94,24 @@
10694
{
10795
'code': r"""
10896
scm> (or)
109-
55275a3300eb11ce47998c15403d99c6
110-
# locked
97+
False
11198
scm> (or (+ 1 1))
112-
e56af2bab40778990634a527fe4407f8
113-
# locked
99+
2
114100
scm> (or False)
115-
55275a3300eb11ce47998c15403d99c6
116-
# locked
101+
False
117102
scm> (define (t) True)
118-
f16aec3dd21e3756aa445ca3e2d11a8d
119-
# locked
103+
t
120104
scm> (or (t) 3)
121-
a87b4aa619df2eba30bc86785b816612
122-
# locked
105+
True
123106
scm> (or 5 2 1)
124-
19a0c723c8c2fa9e2860916af61035e6
125-
# locked
107+
5
126108
scm> (or False (- 1 1) 1)
127-
346bede49af9f8a0fafbc46acfa3395c
128-
# locked
109+
0
129110
scm> (or 4 True (/ 1 0))
130-
8ad686581488b3cc40d870a8db32810e
131-
# locked
111+
4
132112
""",
133113
'hidden': False,
134-
'locked': True
114+
'locked': False
135115
},
136116
{
137117
'code': r"""

Project/scheme/tests/14.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010
.... ((> 2 4) 6)
1111
.... ((< 2 5) 7)
1212
.... (else 8))
13-
047f42cf449bafcfed749a052e47904a
14-
# locked
13+
7
1514
scm> (cond ((> 2 3) 5)
1615
.... ((> 2 4) 6)
1716
.... (else 8))
18-
7160af5f6893bd49bb63dee6b8320930
19-
# locked
17+
8
2018
""",
2119
'hidden': False,
22-
'locked': True
20+
'locked': False
2321
}
2422
],
2523
'scored': True,

Project/scheme/tests/15.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,34 @@
77
{
88
'code': r"""
99
scm> (define x 1)
10-
9d01e356a925e61e19645aef1b1fdd64
11-
# locked
10+
x
1211
scm> (let ((x 5))
1312
.... (+ x 3))
14-
7160af5f6893bd49bb63dee6b8320930
15-
# locked
13+
8
1614
scm> x
17-
2894dd5fa65c8aa8f2b9d920d0e542e0
18-
# locked
15+
1
1916
""",
2017
'hidden': False,
21-
'locked': True
18+
'locked': False
2219
},
2320
{
2421
'code': r"""
2522
scm> (let ((a 1) (b a)) b)
26-
87c30138f7979b4f5a454aacfb191b98
27-
# locked
28-
# choice: SchemeError
29-
# choice: 1
30-
# choice: x
31-
# choice: y
23+
SchemeError
3224
""",
3325
'hidden': False,
34-
'locked': True
26+
'locked': False
3527
},
3628
{
3729
'code': r"""
3830
scm> (let ((x 5))
3931
.... (let ((x 2)
4032
.... (y x))
4133
.... (+ y (* x 2))))
42-
5fb541ba24eaf504edc7eae61bda4f97
43-
# locked
34+
9
4435
""",
4536
'hidden': False,
46-
'locked': True
37+
'locked': False
4738
},
4839
{
4940
'code': r"""

Project/scheme/tests/16.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@
77
{
88
'code': r"""
99
scm> (define y 1)
10-
17cb57908ef08e52f95d9f071151342f
11-
# locked
10+
y
1211
scm> (define f (mu (x) (+ x y)))
13-
0f95edcd87e28fa8e0cb767981adc36d
14-
# locked
12+
f
1513
scm> (define g (lambda (x y) (f (+ x x))))
16-
f52d1b972c48783316eb00d88d4d300f
17-
# locked
14+
g
1815
scm> (g 3 7)
19-
82b58f6e2f61a1a4b7dee17cb8ac08e0
20-
# locked
16+
13
2117
""",
2218
'hidden': False,
23-
'locked': True
19+
'locked': False
2420
}
2521
],
2622
'scored': True,

Project/scheme/tests/19.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@
77
{
88
'code': r"""
99
scm> (let-to-lambda 1)
10-
2894dd5fa65c8aa8f2b9d920d0e542e0
11-
# locked
10+
1
1211
scm> (let-to-lambda 'a)
13-
bcba5a8b5eb148269b808d42d0d4fef5
14-
# locked
12+
a
1513
scm> (let-to-lambda '(+ 1 2))
16-
07bf87256cd8dfa90c86841a1d37643a
17-
# locked
14+
(+ 1 2)
1815
scm> (let-to-lambda '(let ((a 1)
1916
.... (b 2))
2017
.... (+ a b)))
21-
a28e3e321fa8868c1949f68b122a8253
22-
# locked
18+
((lambda (a b) (+ a b)) 1 2)
2319
""",
2420
'hidden': False,
25-
'locked': True
21+
'locked': False
2622
},
2723
{
2824
'code': r"""
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-830 Bytes
Binary file not shown.
-100 Bytes
Binary file not shown.
-341 Bytes
Binary file not shown.
-202 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)