Due by 11:59pm on Wednesday, 4/15
Download hw08.zip. Inside the archive, you will find a file called hw08.scm, along with a copy of the OK autograder.
Submission: When you are done, submit with
python3 ok --submit
. You may submit more than once before
the deadline; only the final submission will be scored.
The ok
program helps you test your code and track your progress.
The first time you run the autograder, you will be asked to log in with your
@berkeley.edu account using your web browser. Please do so. Each time you run
ok, it will back up your work and progress on our servers.
You can run all the doctests with the following command:
python3 ok
To test a specific question, use the -q
option with the
name of the function:
python3 ok -q <function>
By default, only tests that fail will appear. If you
want to see how you did on all tests, you can use the -v
option:
python3 ok -v
If you do not want to send your progress to our server or you have any
problems logging in, add the --local
flag to block all
communication:
python3 ok --local
When you are ready to submit, run ok
with the
--submit
option:
python3 ok --submit
Readings: You might find the following references useful:
The Scheme interpreter is included in the starter ZIP archive. To run the Scheme interpreter, use the following command:
python3 scheme
To load a file (such as hw08.scm
), use
python3 scheme -load hw08.scm
You can also use our online Scheme interpreter.
Write the function deep-map
, which takes a function fn
and a list s
that
may contain numbers or other lists. It returns a list with identical structure
to s
, but replacing each non-list element by the result of applying fn
on
it, even for elements within sub-lists. Assume that the input has no dotted
(malformed) lists.
(define (deep-map fn s)
; YOUR-CODE-HERE
nil
)
Hint: You can use the predicate list?
to check if a value is a list.
To see examples and test your implementation, run
python3 ok -u -q deep-map
Write a procedure substitute
that takes three arguments: a list s
, an old
word, and a new
word. It returns a list with the elements of s
, but with
every occurrence of old
replaced by new
, even within sub-lists.
(define (substitute s old new)
; YOUR-CODE-HERE
nil
)
To see examples and test your implementation, run
python3 ok -u -q substitute
Write sub-all
, which takes a list s
, a list of old
words, and a
list of new
words; the last two lists must be the same length. It returns a
list with the elements of s
, but with each word that occurs in the second
argument replaced by the corresponding word of the third argument.
(define (sub-all s olds news)
; YOUR-CODE-HERE
nil
)
To see examples and test your implementation, run
python3 ok -u -q sub-all