Lab 11: Macros
Due at 11:59pm on Friday, 11/09/2018.
Lab Check-in 7 questions here.
Starter Files
Download lab11.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the Ok autograder.
Submission
By the end of this lab, you should have submitted the lab with
python3 ok --submit
. You may submit more than once before the
deadline; only the final submission will be graded.
Check that you have successfully submitted your code on
okpy.org.
- To receive credit for this lab, you must complete Questions 1 through 2 in lab11.scm (Questions 2 and 3 on this page) and submit through Ok.
Q1: REPL
Sign up for checkoffs in your lab if you'd like to get credit for this week's checkoff.
Explain REPL. What are the different stages and what is done in each stage?
Q2: WWSD: Macros
One thing to keep in mind when doing this question, builtins get rendered as so
scm> +
#[+]
scm> list
#[list]
Use Ok to test your knowledge with the following "What Would Scheme Display?" questions:
python3 ok -q wwsd-macros -u
scm> (define-macro (f x) (car x))
______f
scm> (f (2 3 4)) ; type SchemeError for error, or Nothing for nothing
______2
scm> (f (+ 2 3))
______#[+]
scm> (define x 2000)
______x
scm> (f (x y z))
______2000
scm> (f (list 2 3 4))
______#[list]
scm> (f (quote (2 3 4)))
______SchemeError
scm> (define quote 7000)
______quote
scm> (f (quote (2 3 4)))
______7000
scm> (define-macro (g x) (+ x 2))
______g
scm> (g 2)
______4
scm> (g (+ 2 3))
______SchemeError
scm> (define-macro (if-else-5 condition consequent) `(if ,condition ,consequent 5))
______if-else-5
scm> (if-else-5 #t 2)
______2
scm> (if-else-5 #f 3)
______5
scm> (if-else-5 #t (/ 1 0))
______SchemeError
scm> (if-else-5 #f (/ 1 0))
______5
scm> (if-else-5 (= 1 1) 2)
______2
Q3: Scheme def
Implement def
, which simulates a python def
statement, allowing you to write code like
(def f(x y) (+ x y))
.
Hint: the previous is equivalent to
(def f (x y) (+ x y))
.Additional hint: use
lambda
in your solution.
(define-macro (def func bindings body)
'YOUR-CODE-HERE
`(define ,func (lambda ,bindings ,body)))
Use Ok to test your code:
python3 ok -q scheme-def