Once you have copied both my files, run the test script from the command line. Sample run (you'll have to substitute the name of your scheme file):% cd cs313/hw6/ // or wherever your scheme file is % cp ~schar/cs313/hw6/* . % chmod 755 testhw6 // make executable (might be needed on your Macs)

If you leave out the problem number, the script runs all 7 tests.% ./testhw6 usage: ./testhw6 hw6.scm [problem] % ./testhw6 hw6.scm 1 (super-reverse ()) = () -- OK (super-reverse (quote (a))) = (a) -- OK (super-reverse (quote (a b))) = (b a) -- OK (super-reverse (quote (a (b)))) = ((b) a) -- OK (super-reverse (quote ((a b) ((c)) (d (e))))) = (((e) d) ((c)) (b a)) -- OK (super-reverse (quote (a (b (c)) (d e)))) = ((e d) ((c) b) a) -- OK ======================= 6 of 6 correct ======================

Like I said in class, make sure that your program doesn't crash my
test script. You no longer need to collect or submit sample output.
However, **before you submit, make sure your code runs through the
entire test script without crashing.** It should report the total
nuber of test cases (out of 50) at the bottom. It's fine if you
don't solve all problems, in particular problem 7, but make sure you
define at least the requested function(s) with the correct number of
arguments, and have them return something, e.g. the empty list or one
of the parameters, e.g.,

If you need help getting the testing code to work, see me in office hours or email me.(define (atom-reverse x) x)

Note that for problem 3a, my test script expects the parameters for member? in
the order 'element' first, 'set' second, e.g., `(member? 3 '(1 2 4))`.

Note that for problem 6, my test script assumes a variable 'empty-bst' has been defined that contains your representation of an empy tree. In case you use an empty list for that, simply add the following line to your code:

(define empty-bst '())

If you want a better command-line editor (so that the up/down arrow keys cycle through previous commands), use the wrapper program "rlwrap", also installed in the lab, when calling scheme:

At the prompt, you can enter Scheme expressions:schar@abe:~> rlwrap scheme MIT/GNU Scheme running under GNU/Linux Type `^C' (control-C) followed by `H' to obtain information about interrupts. [...] 1 ]=>

To load a file, use "load" and put the filename in double quotes:1 ]=> (cdr '(a b c)) ;Value 1: (b c)

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.scm1 ]=> (load "hw6") ;Loading "hw6.scm"

For documentation on Scheme, read Sethi chapters 10.1-3 and 15.6. Also, take a look at the Revised(5) Report on Scheme.

- We discussed the function reverse in class. Write a new function
super-reverse that recursively reverses a list and all its sublists.
For example:

You may wish to define the function atom? that tests if its argument is atomic (i.e., not a pair):> (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)

(define (atom? x) (not (pair? x)))

- [Sethi #10.9 (b) and (c) on page 419] Provide your own implementations
of the built-in functions "list-ref" and "list-tail". Use the
names "listRef" and "listTail".

(listRef mylist n) should return element n of mylist, counting from 0. This can be done recursively as follows: For n = 0, listRef should return the car of the list. Otherwise, listRef should return item (n-1) of the cdr of the list. For example:> (listRef '(3 51 9 5) 0) 3 > (listRef '(3 51 9 5) 2) 9 > (listTail '(3 51 9 5) 2) (9 5)

- [Sethi #10.7 (a), (b), (c), (d) on page 419]
Suppose that sets are implemented as lists, where each element of a set
appears exactly once in its list.
- Define a function
`member?`that tests whether an element is a member of a set. Use the function`equal?`to test whether two elements are equal. - Define a function
`union`to construct the union of two sets - Define a function
`intersection`to construct the intersection of two sets - Define a function
`difference`to construct the difference between two sets, i.e., all elements that are in the first set but not in the second set.

- Define a function
- [Sethi #10.4 (d) on page 418] Define a function
`repeats`that takes a list and counts the number of adjacent repeated elements, returning a list containing the counts as follows:> (repeats '(a b a a a c c)) ((1 a) (1 b) (3 a) (2 c)) > (repeats '(a a a a a)) ((5 a)) > (repeats '()) ()

- [Sethi #10.5 (a) and (b) on page 419]
Define tail-recursive functions
`add`and`multiply`that compute the sum and product of all the elements in a list. For example:> (add '(2 4 10 100)) 116 > (multiply '(2 3 5 7)) 210

- 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) #f > (traverse t) (2 3 5 7)

- (Almost) optional challenge problem, only worth 2 points out of 50:

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:

You may want to define helper functions for this one.> (atom-reverse '(a (b c (d)) ((f g) i))) (i (g f (d)) ((c b) a))