Instructions for students & labbies: Students use DrScheme, following the design recipe, on the exercises at their own pace, while labbies wander among the students, answering questions, bringing the more important ones to the lab's attention.
Accumulators on Trees
We've seen some examples of using accumulators with lists. While we've formally introduced the idea this week, a few examples have popped up earlier without any fanfare. What about with other data types? Is there anything additional to see and learn? Yes!
Skim section 32.1 from the book. There are two important points to take away from this section:
- When there are multiple recursive calls, threading the accumulator from one recursive call into the other recursive call is very useful. (See Figure 92.)
- There are multiple ways to use accumulators. One general technique is for the accumulator to be a partial result. When you are done with all of your input (i.e., the base case), you return the accumulator, since that is your result. (E.g., Figure 92.) Another general technique is for the accumulator to build up information that helps the computation. (E.g., 93.)
Accumulator exercise
Develop an accumulator based function that multiplies all the numbers in a tree. This will use the accumulator threading mentioned above. We'll use the following binary tree type.
; A binary tree of numbers (btn) is one of ; - a number, or ; - (make-node n l r), ; where n is a number, and l and r are btns. (define-struct node (n l r)) In addition, we want to take advantage of our mathematical knowledge that anything multiplied by zero is zero. Your function should quit recurring through the tree and return zero if you ever multiply by zero. |
Vector Basics
We frequently want a data structure to store a collection of data. Today we'll introduce one more solution, vectors, for this problem. It's useful to contrast vectors with structures and lists.
| Data structure | # of items | Accessing data |
|---|---|---|
| Structures | Any particular data structure type contains a fixed number of items. E.g., a "posn" contains two items, and make-posn takes two items. |
You can access any of the structure's items directly, by name. E.g., for a "posn", use posn-x and posn-y. |
| Lists | Lists can contain an arbitrary number of items. Given a list, we can easily make a larger one with cons, or a shorter one with rest. |
Accessing the ith item requires about i steps. |
| Vectors | Vectors can contain an arbitrary number of items. But, given a vector, we cannot directly make a larger or shorter one. When creating a particular vector, we give it a specific size that cannot be changed. | You can access any of the vector's items directly, by position number, or index. |
Note: Some languages use the terms arrays instead of vectors.
To use vectors in DrScheme, change to the Advanced Language Level.
Vector examples/exercises
We'll introduce how to use vectors by examples.
|
You've summed up lists in many ways. It's a conceptually simple computation that has been useful for illustrating structural recursion on lists, using abstract functions like foldr and
foldl, and will soon be used to illustrate accumulators. Now, let's sum up vectors.
A vector can be of arbitrary size, but they aren't defined recursively, so how can we write such a function? The indices are natural numbers, which are defined recursively, so let's recur on the index! For example,
; vec-sum-help : vector-of-numbers natural -> number
; Returns the sum of the first num-elts elements in vector v.
(define (vec-sum-help v num-elts)
(cond
[(zero? n) 0]
[(positive? n) (+ (vector-ref v (sub1 num-elts))
(vec-sum-help v (sub1 num-elts)))]))
; vec-sum : vector-of-numbers -> number
; Returns the sum of the elements in vector v.
(define (vec-sum v)
(vec-sum-help v (vector-length v)))
|
|
In math, we often generalize one-dimensional vectors to two- (or more) dimensional matrices. Scheme, like many languages, do not have two-dimensional vectors/matrices. Instead, we can use vectors of vectors. For example,
(define matrix (vector (vector 1 2 3)
(vector 4 5 6)
(vector 7 8 9)))
(define v-of-v (vector (vector 1 2)
(vector 3)
(vector 4 5 6 7)
(vector 8 9)))
As you can see, the inner vectors need not be of the same length in general.
|
This work is licensed under a Creative Commons Attribution 2.5 License. Please follow our