CS 330 Home Schedule Resources

Homework 7
Recursive Programming With Lists.

Estimated time: 3 hours

Acceptance tests for HW 7

The objective of this homework is to practice recursive programming techniques on lists. Do not forget to use invariants to guarantee the correctness of your functions.

Exercises

  1. Recall: A list is:

    Formally, the syntax of a list (in EBNF notation, covered in the textbook):

    <List T> ::= nil | T '|' <List T>

    The notation [a b c] is in fact a shorthand to denote the list made up of the three elements A B and C. This list can also be written A|(B|(C|nil)). This notation emphasizes the recursive definition of a list.

  2. Build two functions, Head and Tail which return, respectively, the head and the tail of a list passed in parameter. (That is, Head returns the head of the list and Tail returns the tail.)

  3. From there, write a function Nth which returns the Nth element of a list passed in as a parameter. Nth returns 0 if the list contains fewer than N elements. Think of the recursive definition of the list to identify the various cases!

  4. Write a recursive function now, Length, which returns the length of a list passed in as a parameter. Think of the recursive definition of the list to identify the various cases!
    For example:
       {Browse {Length [r a p h]}}     % displays 4
    {Browse {Length [[r a p] h]}} % displays 2
    {Browse {Length [[r a p h]]}} % displays 1

    Is your function tail-recursive? If not, make it tail-recursive using an accumulator. What is the invariant which you chose?
  5. Some general functions on lists. Here is a list of commonly used functions on lists. Choose TWO to implement, and program by reasoning about them in an inductive way on the structure of the lists. Do NOT use the built-in version. Try to guess the answer to the last example for each function, then lookup up the built-in version (or the closest you can find) for all five, and use it to check your guess. Submit the code you run and its output.

  6. Let us multiply. Write a function MultList such that { MultList L } returns the result of the multiplication of the elements of the list L. For example,

         { Browse { MultList [ 1 2 3 4 ] } }  % displays 24 
    Write a function now MultList2 such that the recursive call is the last statement executed (tail-recursive). Use an accumulator. Formalize the invariant which is associated with it.
  7. Pattern Matching. Pattern matching ( form correspondence ) is a test which consists in comparing a value with a given pattern.
    For example, let us consider the instruction

    case X
       of nil then instruction1
       [] Head|Tail then instruction2
    end


    X is the value which one tries to match. If X is equal to the empty list  (nil) instruction 1 will be carried out. If not, if X has the shape of a nonempty list (with a head and a tail (cfr. definition of the list), instruction 2 will be carried out.

    Write a function ListType which returns emptylist if the variable passed in as a parameter is an empty list, nonemptylist if the variable is a nonempty list and other if the variable is of another type (integer, atom...).

  8. Rewrite the function MultList by using pattern matching. Call it PMultList.

  9. Factorial numbers!
    Write a function Fact such that { Fact N } returns a list of the first N factorial numbers in ascending order. For example,
         { Browse { Fact 4 } }  % displays [ 1 2 6 24 ] 
  10. Feedback.