CS 119 Lab 2 – Recursion

Objectives

  • Become familiar with recursion
  • Introduce the trace and debugging features in Edscheme
  • Perform the following tasks in the order given.

    1. Evaluate the files words.scm, quilting.scm, and lab2.scm. In the file lab2.scm, look at the two recursive functions reverse1 and reverse2 which both take a word as argument and return the word with the letters in reverse order. We will examine these functions with a useful debugging tool called trace.

    Enter the command:
        (trace reverse1)

    Now, when we invoke the function reverse1, an Inspector window comes up which shows you each time the function was called. You can use the Value button to see what value each function call returned. The indentation shows the level of recursion. Try this out with reverse1.

    Notice in the definition of reverse2, the trace is already placed inside. This is because we are probably interested in tracing reverse-helper, rather than reverse2.   Try typing (trace reverse-helper) at the scheme prompt and see what happens. Why did that happen?

    Now try using the function reverse2. Notice how the recursion differs from the recursion of reverse1.

    Assignment:
    Write out the behavior of reverse1 and reverse2 with the substitution model. Compare your models with the traces of each function.

     

    2. Look at the function downup. We want this function to behave as follows:

    Þ (downup ‘cake)
    (cake ake ke e ke ake cake)

    Þ (downup ‘a)
    (a)

    If you try this however, you will notice that there is a mistake in the definition of downup.

    Assignment:
    Fix the mistake in downup. You may find it useful to use trace to find out what is going on.

     

    3. We want a function explode which behaves as follows:

    Þ (explode ‘dynamite)
    (d y n a m i t e)

    The function takes a word as its argument and returns the sentence containing the single letters of this word.

    Assignment:
    Write both a linear recursive version of explode (like reverse1) and a tail recursive version (like reverse2).

     

    4. Section 2.4 in your text describes custom-sized quilts. Take the file containing your previously defined quilt functions and extend it as follows:

    Assignment:
    Do exercises 2.13 and 2.14 in the text.
    Feel free to design your own quilt patterns. I will display some of the best designs.

     

    5. Email your files containing the assignments to jzimmerm@goucher.edu.