Section Notes 5
CS51—Spring 2009
Week of March 2, 2009
Outline
1. Higher order functions review
2. Complexity and BigO
3. Induction
4. Exam sample question
5. Design note
1
Goals for today:
At the end of today’s section, you should be able to do the following:
1. Brag to your friends that your mind is of a higher order... and mean it.
2. Explain to a nontechnical person how we analyze the complexity of an operation.
3. Write a recursive function (you already know how to do this).
4. Prove that your recursive function is correct.
5. Extract a recurrence relation for your function.
6. Determine the runtime of your function based on the above.
This week’s section will revisit higher order functions and then will focus on what we can learn by
analyzing our programs. The former is a tool for writing programs, the latter is a tool for determining that
they are correct and efficent.
2
Higher order functions
A
higherorder function
is a function that takes functions as arguments. We will focus on
foldr
,
filter
,
and
map
, which implement common patterns in functional programming.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2.1
Fold
Reviewing
foldr
: We usually think of
foldr
as combining the elements of a list to produce a nonlist, for
example computing the sum of a list of numbers. However, given the right function,
foldr
can also build
lists. This effect is achieved by providing a combining operation and a base value that will be used to process
the input list. When we call
foldr
we provide three arguments: a binary function we will call
new
cons
, a
value we will call
base
case
, and our input list
lst
(i.e.
(fold new
cons base
case lst)
).
The best way of thinking about foldr is to envision deconstructing a list and replacing the cons holding
the list together with the intended function. That is, the following list
(define lst (list 1 2 3 4))
is
actually:
(cons 1 (cons 2 (cons 3 (cons 4 empty()))))
. The base case of fold applies where the empty
list would be located. For example, summing
lst
would yield 10, which we obtained by constructing the
following replacement
(+ 1 (+ 2 (+ 3 (+ 4 0))))
.
We can see that 0 is the base case.
Therefore, the
function sum would be written as follows:
(define (sum lst) (foldr + 0 lst))
.
The function that replaces the cons can actually still be cons, which would construct a new list.
In
addition, the function can be written as a lambda that takes 2 arguments  the first of the list that’s
returned and the rest of the list that’s returned. Of course, neither of these arguments have to be explicity
used in the lambda function; they just have to be written as part of the function because foldr expects the
lambda function to take 2 arguments. Why can’t foldr work otherwise?
Here we will use it to write
flatten
. The
flatten
operation takes a list of lists:
((1 2) (3 4) (5 6) (7 8))
and removes the nesting, leaving us with just the “leaves”:
(1 2 3 4 5 6 7 8)
We can do this by folding with a listbuilding function. Which one do we need?
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 GREGMORRISETT
 Recursion

Click to edit the document details