Comp 210
Due: At the beginning of class on 98.Jan.16 (Wed.)
Revised: Now due 98.Jan.18 (Fri.)
;; fact: num -> num ;; Return n!, which stands for n*(n-1)*(n-2)*...*3*2*1 ;; (define fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))) ;; multfib: num -> num ;; A multiplicative version of the fibonacci sequence. ;; (define multfib (lambda (n) (if (< n 3) n (* (multfib (- n 1)) (multfib (- n 2)))))) ;; threes: num -> num ;; Returns 1??? ;; (define threes (lambda (n) (if (< n 2) 1 (threes (if (even? n) (/ n 2) (+ 1 (* n 3)))))))(These functions also appear in the file
~comp210/Assignments/hw02-problem1.ss
.)
Evaluate each of these functions for values of n between 0 and 6 inclusive. For each value of n, use Donkey to determine the total number of calls made to each function. (The initial call counts, so the answer for n = 0 is 1 in each case.) The point of this exercise is to see how scheme evaluates recursive functions; think about what is happening at each step!
For a small amount of extra credit, estimate the number of recursive calls made by each function in terms of n. (Warning: I don't know the answer for threes.)
handle-lon
).
(cons symbol (cons value empty))
.
(What are some examples?)
Define an Association List (i.e., a list of Associations).
Using the design recipe,
write the function lookup,
which takes a symbol and an Association List.
It searches the association list until it finds an association whose
key is the same as its first argument.
If it finds such an association, it returns the value (second element) from
the association.
If not, it returns¹
#f
.
Hint:
Your code will be easier to read and to write if you
write the selectors association-key
and association-datum
(each of which are
very simple: they take an association, and return the
corresponding values inside. What do the contracts look like?)
#f
?
Is there a way around the problem?
(back)