0% found this document useful (0 votes)
69 views8 pages

Lab 12 Solutions CS 61A Summer 2020 PDF

CS_61A_Summer_2020

Uploaded by

oihso069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views8 pages

Lab 12 Solutions CS 61A Summer 2020 PDF

CS_61A_Summer_2020

Uploaded by

oihso069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

8/7/2020 Lab 12 Solutions | CS 61A Summer 2020

Lab 12 Solutions lab12.zip (lab12.zip)

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

What Would Scheme Display?


Q1: WWSD: Macros
One thing to keep in mind when doing this question, builtins get rendered as such:

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> (define-macro (f x) (car x))


______

scm> (f (2 3 4)) ; type SchemeError for error, or Nothing for nothing


______

scm> (f (+ 2 3))
______

scm> (define x 2000)


______

scm> (f (x y z))
______

scm> (f (list 2 3 4))


______

scm> (f (quote (2 3 4)))


______

scm> (define quote 7000)


______

scm> (f (quote (2 3 4)))


______

scm> (define-macro (g x) (+ x 2))


______

scm> (g 2)
______

scm> (g (+ 2 3))
______

scm> (define-macro (h x) (list '+ x 2))


______

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> (define-macro (if-else-5 condition consequent) `(if ,condition ,consequent 5))


______

scm> (if-else-5 #t 2)
______

scm> (if-else-5 #f 3)
______

scm> (if-else-5 #t (/ 1 0))


______

scm> (if-else-5 #f (/ 1 0))


______

scm> (if-else-5 (= 1 1) 2)
______

Q2: WWSD: Quasiquote


Use Ok to test your knowledge with the following "What Would Scheme Display?" questions:

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> `(1 (,x) 3)


______

scm> `(1 ,(+ x 2) 3)


______

scm> (define y 3)
______

scm> `(x ,(* y x) y)


______

scm> `(1 ,(cons x (list y 4)) 5)


______

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.

Note: the previous is equivalent to (def f (x y) (+ x y)) .

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

(define-macro (def func args body)


`(define ,func (lambda ,args ,body)))

Use Ok to test your code:

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)))))

Do not de ne any other helper functions.

(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))
)

Use Ok to test your code:

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))) .

scm> (define (square x) (* x x))


square
scm> (define (add-one x) (+ x 1))
add-one
scm> (define (double x) (* x 2))
double
scm> (define composed (compose-all (list double square add-one)))
composed
scm> (composed 1)
5
scm> (composed 2)
17

(define (compose-all funcs)


(lambda (x)
(if (null? funcs)
x
((compose-all (cdr funcs)) ((car funcs) x))))
)

Use Ok to test your code:

python3 ok -q compose-all

Streams
Q6: Partial sums
De ne a function partial-sums , which takes in a stream with elements

a1, a2, a3, ...

and outputs the stream

a1, a1 + a2, a1 + a2 + a3, ...

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.

(define (partial-sums stream)


(define (helper sum-so-far stream-remaining)
(if (null? stream-remaining)
nil
(begin (define new-sum (+ sum-so-far (car stream-remaining)))
(cons-stream new-sum (helper new-sum (cdr-stream stream-remaining))))))
(helper 0 stream)
)

Use Ok to test your code:

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)

O ce Hours (/o ce-hours.html)

Sta (/sta .html)

Resources (/resources.html)
Studying Guide (/articles/studying.html)

Debugging Guide (/articles/debugging.html)

Composition Guide (/articles/composition.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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy