Mini Quiz 2 Solutions
Solutions: You can find the file with solutions for all questions here.
To begin the quiz, use the following ok command:
python3 ok -u
Question 1: WWSP: Scheme Lists
Use OK to test your knowledge with the following "What Would Scheme Print?" questions:
python3 ok -q wwsp-lists -u
scm> (define lst (list () '(1 . 2) '(1 2 . (3))))
_______
scm> (cons (car lst) (car (cdr lst)))
_______
scm> (cons (cdr (car (cdr lst))) (car lst))
_______
scm> (list (car lst) (cdr lst))
_______
scm> (append (car lst) (cdr lst))
_______
scm> (cons (cdr lst) (car lst))
_______
Question 2: Deep Map
Write the function deep-map
, which takes a function fn
and a nested list
s
. A nested list is a list where each element is either a number or a list
(e.g. (1 (2) 3)
where 1
, (2)
, and 3
are the elements). 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. For example:
scm> (define (double x) (* 2 x))
double
scm> (deep-map double '(2 (3 4)))
(4 (6 8))
Assume that the input has no dotted (malformed) lists.
Hint: You can use the functions
map
andlist?
, which checks if a value is a list.
(define (deep-map fn s)
(cond ((null? s) s)
((list? (car s)) (cons (deep-map fn (car s))
(deep-map fn (cdr s))))
(else (cons (fn (car s))
(deep-map fn (cdr s))))))
Use OK to test your code:
python3 ok -q deep-map
After you have unlocked all of the cases, submit your responses with:
python3 ok -v --submit