[Index of Lectures]

[ L01 L02 L03 L04 L05 L06 L07 L08 L09 L10 L11 L12 L13 L14 L15 L16 L17 L18 L19 L20 L21 L22 L23 ]

Lecture 06—Arbitrary-sized data, self reference, and natural recursion

Before Lecture

At this point in the course, you are responsible for everything in the following edX modules: Welcome to 110, BSL, HtDF, HtDD, HtDW and Compound. (That includes videos, online questions, practice problems, lecture problems, lecture discussion.)

To prepare for this lecture, please work through: Introduction to Arbitrary sized data and List Mechanisms in the Self-Reference module on edX. Be sure to solve all the online questions, since one of them may be our clicker question.

During Lecture

One of the things that makes computers useful is their ability to operate on large amounts of information. But up to this point in the course all of our data definitions have described data of fixed size. That changes in this lecture.

By the end of the lecture—including the post-lecture work—you should be able to:

The starters for this lecture are:

After Lecture

Here are the solutions for this lecture:

Wow! That was pretty interesting.

Self-reference and natural recursion is perhaps the most interesting part of the course. We will keep using these techniques throughout the rest of the course.

Below are some notes that can help you master this material. It is crucial that you do so—in the next lecture, we will do a world program with lists!

First, note that what we have been learning about templating really helps us. Because even though recursion may seem a bit confusing at first, by following the revised recipe it turns out to be fairly easy to write a working recursive function. The template already has the recursion we need in the natural place for it.

You should review the recipe pages, but the recipe revisions, in short, are:

The reason you can trust the natural recursion is that the type comments and template rules are based on type theory, so the natural recursions are theoretically sound. This is how designers and engineers often work, they know that a theoretical result makes a certain way of thinking about the problem sound and then they work that way. This is thus software engineering based on results in programming language research.

BUT... you might want to "look inside" at the recursion a little bit right now. If so, put the following into a Racket tab, select the BSL language, and use the Stepper button to see how it runs.

(define (sum lon) 
  (cond [(empty? lon) 0] 
        [else 
         (+ (first lon) 
            (sum (rest lon)))]))

(sum (cons 1 (cons 2 (cons 3 empty))))
    

While that will show you how the recursion runs, we want to stress that the more sophisticated way of thinking about it is to consider one call to the function, trust the natural recursion to produce the correct result, and then work out what the function should do with that result.

To review:

Be sure to complete the before lecture section of Lecture 7 before that lecture.