CS 313 - Homework 6 - Scheme
Due: Wednesday 4/8 in class
The purpose of this assignment is to introduce you to the joys of
functional programming. You will have to write several functions in
Scheme. This is not a group project - you'll have to work by
yourself. As usual, on the first page of your printout, write your
name, the names of the students with whom you collaborated, and how
much time it took you. No electronic submission this week - but
as always be sure to include sample output!
Scheme on the lab machines
A Scheme interpreter called MIT Scheme is installed on the lab
machines. To run it, simply type "scheme":
schar@abe:~> scheme
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
[...]
1 ]=>
At the prompt, you can enter Scheme expressions:
1 ]=> (cdr '(a b c))
;Value 1: (b c)
To load a file, use "load" and put the filename in double quotes:
1 ]=> (load "sample")
;Loading "sample.scm"
If you get an error, you'll get a different prompt. To get back to the
main prompt, type control-G. To exit scheme, type control-D.
For an example scheme input file, see my file
sample.scm
sample.scm in ~schar/cs313/scheme/.
For documentation on Scheme, read Sethi chapters 10.1-3 and 15.6.
Also, take a look at the
Revised(5) Report on Scheme.
Your assignment:
Implement the following functions. Hand in a printout of your code.
Include sample output for each function! (You can simply cut
and paste from a Scheme session into a text file.)
- We discussed the function reverse in class. Write a new function
super-reverse that recursively reverses a list and all its sublists.
For example:
> (reverse '(a (b c (d)) ((f g) i)))
(((f g) i) (b c (d)) a)
> (super-reverse '(a (b c (d)) ((f g) i)))
((i (g f)) ((d) c b) a)
You may wish to define the function atom? that tests if its argument
is atomic (i.e., not a pair):
(define (atom? x)
(not (pair? x)))
- Do #10.9 (b) and (c) on page 419 in Sethi. Use the names
"listRef" and "listTail", since "list-ref" and "list-tail" are
already defined. For listRef, follow the convention that the
elements of the list are numbered beginning with 0. The method
for computing the nth element is the following:
For n = 0, listRef should return the car of the list.
Otherwise, listRef should return the (n-1)st item of the cdr of the list.
For example:
> (listRef '(3 51 9 2) 0)
3
> (listRef '(3 51 9 2) 2)
9
- Do #10.7 (a), (b), (c), (d) on page 419 in Sethi.
- Do #10.4 (d) on page 419 in Sethi.
- Do #10.5 (a) and (b) on page 419 in Sethi.
- Implement a binary search tree in scheme, including functions
(insert n bst), (present? n bst), and (traverse bst). Inserting a
value should not modify the tree, but rather return a new tree. The
function present? should test membership (the name member? is already
used in problem 3). The function traverse should return a list of all
the values in the tree using an in-order traversal. You can come up
with your own representation for BST nodes (hint: think lists).
Sample usage:
> (define t (insert 3 (insert 5 (insert 2 (insert 7 empty-bst)))))
> (present? 3 t)
#t
> (present? 4 t)
() ;; i.e. #f
> (traverse t)
(2 3 5 7)
- This problem is required for those who already know Scheme or
LISP (e.g.,
from taking CS 311), and optional / extra credit for those who don't:
Write a function atom-reverse that is similar to super-reverse except it
only reverses the order of the atoms, while maintaining the original list
structure. For example:
> (atom-reverse '(a (b c (d)) ((f g) i)))
(i (g f (d)) ((c b) a))
You may want to define helper functions for this one.
- How much time did it take you?