Homework 9
Due by 11:59pm on Monday, 8/3
Instructions
Download hw09.zip. Inside the archive, you will find a file called hw09.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. See Lab 1 for instructions on submitting
assignments.
Using OK: If you have any questions about using OK, please refer to this guide.
Readings: You might find the following references useful:
Required questions
Question 1
Define the procedures cadr and caddr, which return the second
and third elements of a list, respectively:
(define (cddr s)
(cdr (cdr s)))
(define (cadr s)
'YOUR-CODE-HERE
nil
)
(define (caddr s)
'YOUR-CODE-HERE
nil
)
Use OK to unlock and test your code:
python3 ok -q cadr-caddr -u
python3 ok -q cadr-caddr
Question 2
Using cond, define a procedure sign that returns -1 for negative
arguments, 0 for zero, and 1 for positive arguments:
(define (sign x)
'YOUR-CODE-HERE
nil
)
Use OK to unlock and test your code:
python3 ok -q sign -u
python3 ok -q sign
Question 3
Implement a function called ordered?, which takes a list of numbers and
returns True if the numbers are in ascending order, and False otherwise.
Hint: You might want to use the built-in
null?function, which takes in one argument and checks if that argument isnil.
(define (ordered? s)
'YOUR-CODE-HERE
nil
)
Use OK to unlock and test your code:
python3 ok -q ordered -u
python3 ok -q ordered
Question 4
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. 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 function
list?to check if a value is a list.
(define (deep-map fn s)
'YOUR-CODE-HERE
nil
)
Use OK to test your code:
python3 ok -q deep-map