CS44 W00: Homework 3

Assigned: Friday, January 28, 2000
Due: Friday, February 4, 2000, in the beginning of class.

1. Exercises 4.1, 4.2 (only c and d), 4.4, 4.7 on pg. 118 R&N

(30 points total---5 + 5 + 10 + 10)

2. Scheme

Rotating Lists

(30 points)

Define the function RotateLeft1, which takes a list as its argument and returns a new list in which the former 1st element becomes the last. For example:

(RotateLeft1 '(a b c d))  -> (b c d a) 
(RotateLeft1 '(a)) -> (a) 
(RotateLeft1 '()) -> ()

Define RotateRight1 like RotateLeft1 except that it rotates the list in the other direction:

(RotateRight1 '(a b c d)) -> (d a b c) 
(RotateRight1 '()) -> ()

Now define Rotate, as a function of two arguments, a list L and an integer N. It returns a new list formed by rotating L N times to the right if N>0 and to the left if N<0. Examples are:

(Rotate '(one two three four five)  2) -> (four five one two three)
(Rotate '(one two three infinity)  -3) -> (infinity one two three)
(Rotate '( () (()) ((())) ) 0) -> (() (()) ((())))

Counting Cons Cells

(20 points)

In class we defined a function COUNTATOMS which computes the number of atoms in a list. Write a similar function countCells which computes the number of list cells a list uses. An atom uses no cells. The empty list (i.e., ()) uses no cells. A list uses one cell for each of its members in addition to the cells each of its members uses. For example:

(countCells  '(one two three four five)) -> 5 
(countCells  '(one (two) three four five)) -> 6
(countCells  '(define (square x) (* x x)) -> 8
(countCells  '()) -> 0
(countCells  'foo) -> 0

Binary search

(20 points)

Implement a function called binary-search that searches a binary tree for the presence of a node. The function should take as parameters a tree and a goal node. The function should return the node if found, and false otherwise. Make sure you tell us which tree representation you use and give us an example of a tree as part of the comments for this function.

What to hand in

Bring your written answers, printouts of your Scheme functions, and test runs for your functions to class on Friday. Email the path to your code organized in a separate directory called hmw3 to popescu@cs.

Grading

Switch to:


vasilis@cs.dartmouth.edu