CS37 -- Lab 3
Due: Thursday, 17 Sep by 11:59 pm
Email solutions and evidence of testing to:
cfk@cs.swarthmore.edu for:
- page 77, exs. 1.41, 1.42, 1.43
- Write a function called list-replace that takes a list of pairs
and a list and returns a new list with the first element of each
pair replaced by the second element of each pair. This problem is
more difficult than it may initially seem. Be sure your function
can handle both test cases below. (The second test case is the
harder one.)
;; Test cases:
; (list-replace '((carrots peas) (fork spoon))
; '(eat your carrots with a fork))
;; => (eat your peas with a spoon)
; (list-replace '((carrots peas) (peas potatoes))
; '(eat your carrots and peas))
;; => (eat your peas and potatoes)
;; NOTE: THE ANSWER IS NOT: (eat your potatoes and potatoes)
- Write a procedure called pairwise-map that takes a procedure of two
arguments and a list, and then executes the procedure on successive
pairs in the list returning the results in a new list.
(pairwise-map + '(2 3 4 5 7)) ==> (5 7 9 12)
(pairwise-map max '(2 4 3 5 4 1)) ==> (4 4 5 5 4)
(pairwise-map * '(2)) ==> ()
- Write a procedure called map2 that takes a procedure of two
arguments and two lists, and then executes the procedure on
successive elements of the lists returning the results in a list.
(map2 + '(1 2 3 4) '(4 3 2 1)) ==> (5 5 5 5)
(map2 * '(3 3 3) '(4 3 2)) ==> (12 9 6)
(map2 / '() '()) ==> ()
- page 110-111, exs. 2.27
- Write a function called add1-to-each* which adds 1 to each element
of a deep list.
; (add1-to-each* '(1 (2 3) (4 (5 6))))
; --> (2 (3 4) (5 (6 7)))
- Write a function called operate-on-each* which returns a function
that performs an operation on each element of a deep list.
; (define double-each* (operate-on-each* (lambda (n) (* n 2))))
; (double-each* '(1 (2 (3))))
; --> (2 (4 (6)))
- Using a database like that in Clab7, write and test procedures
to get a) a person's name from account number, b) a person's balance
from name, c) find the sum of all balances in the database, and d)
make a deposit using a person's account number. The parameters
to the deposit function should be the account number, the ammount
of the deposit, and the database being used. Note that because our
auditors require an audit trail, we never destroy our old database.
If an operation is going to 'change' the database, we simply return
a 'new' database with the change realized in the new database.