Lab 12 Solutions CS 61A Summer 2020 PDF
Lab 12 Solutions CS 61A Summer 2020 PDF
Solution Files
Topics
Consult this section if you need a refresher on the material for this lab. It's okay to skip directly to the
questions and refer back here should you get stuck.
Required Questions
scm> +
#[+]
scm> list
#[list]
If evaluating an expression causes an error, type SchemeError . If nothing is displayed, type Nothing .
Use Ok to test your knowledge with the following "What Would Scheme Display?" questions:
python3 ok -q wwsd-macros -u
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 1/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
scm> +
______
scm> list
______
scm> (f (+ 2 3))
______
scm> (f (x y z))
______
scm> (g 2)
______
scm> (g (+ 2 3))
______
scm> (h (+ 2 3))
______
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 2/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
scm> (if-else-5 #t 2)
______
scm> (if-else-5 #f 3)
______
scm> (if-else-5 (= 1 1) 2)
______
python3 ok -q wwsd-quasiquote -u
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 3/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
scm> '(1 x 3)
______
scm> (define x 2)
______
scm> `(1 x 3)
______
scm> `(1 ,x 3)
______
scm> '(1 ,x 3)
______
scm> `(,1 x 3)
______
scm> `,(+ 1 x 3)
______
scm> (define y 3)
______
Macros
Q3: Scheme def
Implement def , which simulates a python def statement, allowing you to write code like (def f(x y) (+ x
y)) .
The above expression should create a function with parameters x and y , and body (+ x y) , then bind it
to the name f in the current frame.
Hint: We strongly suggest doing the WWPD questions on macros rst as understanding the rules of
macro evaluation is key in writing macros.
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 4/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
python3 ok -q scheme-def
Streams
Q4: Multiples of 3
De ne implicitly an in nite stream all-three-multiples that contains all the multiples of 3, starting at 3.
For example, the rst 5 elements should be: (3 6 9 12 15)
You may use the map-stream function de ned below. map-stream takes in a one-argument function f and
a stream s and returns a new stream containing the elements of s with f applied.
(define (map-stream f s)
(if (null? s)
nil
(cons-stream (f (car s)) (map-stream f (cdr-stream s)))))
(define (map-stream f s)
(if (null? s)
nil
(cons-stream (f (car s)) (map-stream f (cdr-stream s)))))
(define all-three-multiples
(cons-stream 3
(map-stream (lambda (x) (+ x 3)) all-three-multiples))
)
python3 ok -q multiples_3
Submit
Make sure to submit this assignment by running:
python3 ok --submit
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 5/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
Optional Questions
Scheme Basics
Q5: Compose All
Implement compose-all , which takes a list of one-argument functions and returns a one-argument
function that applies each function in that list in turn to its argument. For example, if func is the result of
calling compose-all on a list of functions (f g h) , then (func x) should be equivalent to the result of
calling (h (g (f x))) .
python3 ok -q compose-all
Streams
Q6: Partial sums
De ne a function partial-sums , which takes in a stream with elements
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 6/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
If the input is a nite stream of length n, the output should be a nite stream of length n. If the input is
an in nite stream, the output should also be an in nite stream.
python3 ok -q partial-sums
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 7/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
8/7/2020 Lab 12 Solutions | CS 61A Summer 2020
CS 61A (/)
Weekly Schedule (/weekly.html)
Resources (/resources.html)
Studying Guide (/articles/studying.html)
Policies (/articles/about.html)
Assignments (/articles/about.html#assignments)
Exams (/articles/about.html#exams)
Grading (/articles/about.html#grading)
This study source was downloaded by 100000877321312 from CourseHero.com on 05-05-2024 13:52:15 GMT -05:00
https://cs61a.org/lab/sol-lab12/ 8/8
https://www.coursehero.com/file/81320571/Lab-12-Solutions-CS-61A-Summer-2020pdf/
Powered by TCPDF (www.tcpdf.org)