fib
, so that (fib n)
returns the n-th Fibonacci number. Translate the
efficient, tail-recursive C version discussed in lecture to
Scheme. fib(n)
should work starting at
n=0
, with fib(0) = 0
,
fib(1) = 1
, and the recurrence
relation taking care of fib(n)
for n >= 2
.nth
, defined such that
(nth alist n)
returns the n-th
element in alist
. Start the indexing
at 1, so (for example),
(nth '(a b c d) 1)
returns
a
, (nth '(a b c d) 2)
returns b
, etc.
If
the list has less than n
entries, have it return the empty
list. (It will be the responsibility of
the user of the function to know if
n
within the bounds of the list.)contains
, defined such that
(contains alist item)
returns
#t
if item
can be
found in any level of alist
,
and #f
otherwise. For example,
(contains '(a (b c) d) 'c)
returns #t
, and
#f
.listleaves
, defined so that
(listleaves bt)
lists the leaves in
the binary tree bt
, using the
representation discussed in class and the text.
For example,(listleaves
'(a (b (c () ()) (d () (f () ()))) (e () ())))
bsearch
, so that
(bsearch item bst)
returns #t
if item
is found in the binary search tree
bst
. Assume that item
is an
integer and that the elements of the tree bst
are
integers. Use the binary search algorithm (not
contains
!). Thus, for example,
(bsearch 3 '(5 (2 (1 () ()) (3 () (4 () ()))) (7 () ())))
returns #t
, and
(bsearch 6 '(5 (2 (1 () ()) (3 () (4 () ()))) (7 () ())))
returns #f
.
TIPS: Scheme can be conveniently developed in several different environments. Racket is probably the most convenient.
Submit: A single file containing all your function definitions (together with any other auxiliary functions you may find useful to define). Use the extension ".rkt" whether or not you use Racket. Indent appropriately and include documentation for each function. Submission location is here.
Back to CS170 Assignments.