CS61A Lab 10
Lab 10: Scheme lab10.zip
Due by 11:59pm on Wednesday, March 30.
Starter Files
Download lab10.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the Ok autograder.
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.
Scheme
Scheme is a famous functional programming language from the 1970s. It is a dialect of Lisp (which stands for LISt Processing). The first observation most people make is the unique syntax, which uses a prefix notation and (often many) nested parentheses (see http://xkcd.com/297/). Scheme features first-class functions and optimized tail-recursion, which were relatively new features at the time.
Our course uses a custom version of Scheme (which you will build for Project 4) included in the starter ZIP archive. To start the interpreter, type
python3 scheme
. To run a Scheme program interactively, typepython3 scheme -i <file.scm>
. To exit the Scheme interpreter, type(exit)
. You may find it useful to try code.cs61a.org/scheme when working through problems, as it can draw environment and box-and-pointer diagrams and it lets you walk your code step-by-step (similar to Python Tutor). Don’t forget to submit your code through Ok though!
Scheme Editor
As you’re writing your code, you can debug using the Scheme Editor. In your scheme
folder you will find a new editor. To run this editor, run python3 editor
. This should pop up a window in your browser; if it does not, please navigate to localhost:31415 and you should see it.
Make sure to run python3 ok
in a separate tab or window so that the editor keeps running.
If you find that your code works in the online editor but not in your own interpreter, it’s possible you have a bug in code from an earlier part that you’ll have to track down. Every once in a while there’s a bug that our tests don’t catch, and if you find one you should let us know!
Expressions
Primitive Expressions
Call Expressions
Special Forms
Control Structures
If Expressions
Cond Expressions
Defining Names
Define Form
Lambda Functions
Lambda Functions
Required Questions
What Would Scheme Display?
Q1: Combinations
Let’s familiarize ourselves with some built-in Scheme procedures and special forms!
Use Ok to unlock the following “What would Scheme print?” questions:
1 python3 ok -q combinations -u
1 | $ python3 ok -q combinations -u |
Coding Questions
Q2: Over or Under
Define a procedure over-or-under
which takes in a number num1
and a number num2
and returns the following:
- -1 if
num1
is less thannum2
- 0 if
num1
is equal tonum2
- 1 if
num1
is greater thannum2
Challenge: Implement this in 2 different ways using
if
andcond
!
1 | (define (over-or-under num1 num2) |
Use Ok to test your code:
1 | python3 ok -q over_or_under✂️ |
Q3: Make Adder
Write the procedure make-adder
which takes in an initial number, num
, and then returns a procedure. This returned procedure takes in a number inc
and returns the result of num + inc
.
Hint: To return a procedure, you can either return a
lambda
expression ordefine
another nested procedure. Remember that Scheme will automatically return the last clause in your procedure.You can find documentation on the syntax of
lambda
expressions in the 61A scheme specification!
1 | (define (make-adder num) |
Use Ok to test your code:
1 | python3 ok -q make_adder✂️ |
Q4: Compose
Write the procedure composed
, which takes in procedures f
and g
and outputs a new procedure. This new procedure takes in a number x
and outputs the result of calling f
on g
of x
.
1 | (define (composed f g) |
Use Ok to test your code:
1 | python3 ok -q composed✂️ |
Q5: Pow
Implement a procedure pow
for raising the number base
to the power of a nonnegative integer exp
for which the number of operations grows logarithmically, rather than linearly (the number of recursive calls should be much smaller than the input exp
). For example, for (pow 2 32)
should take 5 recursive calls rather than 32 recursive calls. Similarly, (pow 2 64)
should take 6 recursive calls.
Hint: Consider the following observations:
- x2y = (xy)2
- x2y+1 = x(xy)2
For example we see that 232 is (216)2, 216 is (28)2, etc. You may use the built-in predicates
even?
andodd?
. Scheme doesn’t support iteration in the same manner as Python, so consider another way to solve this problem.
1 | (define (pow base exp) |
Use Ok to unlock and test your code:
1 | python3 ok -q pow -u |
Submit
Make sure to submit this assignment by running:
1 | python3 ok --submit |