Computer Science 60
Principles of Computer Science
Fall 2003


Assignment 6 Lite: Delayed Evaluation and Tail Recursion!
Due Monday, October 13 by 11:59 PM

This assignment is "liter" than usual, due to the upcoming exam on October 14. The assignment is worth 70 points (rather than the usual 100).

This assignment comprises two parts. In the first part you'll get a chance to explore rex's delayed evaluation mechanism. The second part involves writing tail-recursive functions. Throughout this assignment, you can use any built-in rex functions that you like.

As indicated below, you will need to submit two separate files: One called part1.rex and the other called part2.rex.

Please use the standard commenting convention of your name, file name, and date in each file. In addition, please have at least a one-line comment for each function that you implement. Finally, please BE SURE TO NAME YOUR FILES AND FUNCTIONS EXACTLY AS WE'VE SPECIFIED. The grading procedure for this assignment is partially automated and files and functions whose spellings differ at all from those below will cause our scripts to "barf".

Part 1: Infinite Lists (45 Points)


In this part of the assignment, you will be writing some very cool functions with infinite lists and delayed evaluation. Please put these functions in a file called part1.rex and submit that file.

Recall that in class we wrote a function called from(N) which takes a number N as input and generates the infinite list of integers greater than or equal to N. The function was written as:

  from(N) => [N |$ from(N+1)];
Another useful function is get(N,L) which takes a non-negative integer N and an arbitrary infinite list L as input and returns the Nth element in the list. This function can be written as:
  get(N, [F | R]) => N == 1 ? F : get(N-1, R);
Notice that this function definition assumes that N is not larger than the length of the list. (This is OK here because the lists we'll be dealing with are, well, um, infinitely long!) Finally, one last function which you'll use is find_index(X, L), similar to what you wrote in Assignment 4. This function takes an element X and an arbitrary list L and returns the index of the first occurence of X in list L. This function can be written as:
  find_index(X, [Y | L]) => X == Y ? 1 : 1 + find_index(X, L);
Notice that this implementation also implicitly assumes that the list is infinitely long. (Otherwise, we would need a base case!)

Now you could use these functions as follows:

rex > get(5, from(10));
14

rex > find_index(5, from(4));
2
Although you won't need these functions to write your code for this assignment, you'll definitely want to use them to make sure that your code works! You'll find these functions already defined in the file /cs/cs60/assignments/assignment6/helpers.rex. (This file does not contain from since that is built-in to rex.)

Part 2: Tail Recursion (25 Points)


Now for tail recursion! Please put the following functions in a file called part2.rex and submit this file. Last modified October 2003 by Ran Libeskind-Hadas