University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 6
(define (recursion)
(recursion))
"...in order to understand recursion, one
must first understand recursion"
Overview of today's lecture
Answers to questions
that have come up
- Handouts today
- This one
- Recursion pictures
Review
- Last week's big ideas: global vs. local variables, and
their scope
- Smaller ideas: We can now write our programs with intuitive
names for variables!
- Now we're ready for a paradigm shift!
Somebody overwrote count,
what do we do?
- We're going to get a huge prize if we can just figure out
the number of words in a sentence
- But someone came in and typed the following (overwriting
the definition of count!):
: (define (count sw) -1)
length
: (count '(a b c))
-1
- The problem is, the sentence is really long. I mean really
long.
- What can we do?
- We'll have to create an alternate procedure my-count,
I guess!
;; Given a sentence, return the number of words
;;
;; e.g.,
;; (my-count '(a b c)) ==> 3
;;
(define (my-count s)
==> (cond ((empty? s) 0)
==> ((empty? (bf s)) 1)
==> ((empty? (bf (bf s))) 2)
==> ((empty? (bf (bf (bf s)))) 3)
==> ... etc ... ))
- Clearly we need something else...drum roll...
Recursion
Recursion: (definition) noun. See
recursion. (courtesy UMEC's
Jargon file)
- You already know what it is, you just didn't know it yet
- Take a look at the handout with the pictures on it. [LevyLapidot2000]
- All the pictures demonstrate recursion of one form or another.
- What do they all have in common?
- Do some seem related to others? How?
Recursion: Formal Definition (courtesy
[NIST:dads])
An algorithmic technique where a function, in order
to accomplish a task, calls itself with some part of the task.
- Recursive solutions involve two major parts:
- Base case(s), in which the problem is simple enough
to be solved directly,
- Recursive case(s). A recursive case has three components:
- Divide the problem into one or more simpler or smaller
parts of the problems,
- Invoke the function (recursively) on each part, and
- Combine the solutions of the parts into a solution
for the problem.
- Depending on the problem, any of these may be trivial or
complex.
Our first example: somebody overwrote
count!
- We're going to get a huge prize if we can just figure out
the number of words in a sentence
- But someone came in and typed the following (overwriting
the definition of count!):
: (define (count sw) -1)
count
: (count '(a b c))
-1
- The problem is, the sentence is really long. I mean really
long.
- What can we do?
- We'll have to create an alternate procedure my-count,
I guess!
(define (my-count s)
(if (empty? s)
0
(+ 1
(my-count (butfirst s)))))
- Testing and modeling it...
: (my-count '())
==> 0
: (my-count '(1 2 3))
==> 3
Another example: nothing-but-a-words?
- Do all the words in a sentence start with a?
- Old way (making use of the HOF all? we wrote earlier):
;; Given a sentence of numbers, return the sum of all the numbers
;;
;; (nothing-but-a-words? '(adam ant and alan alda)) ==> #t
;; (nothing-but-a-words? '(an apple a day)) ==> #f
;;
(define (a-word? w) (equal? (first w) 'a))
(define (all? pred? s)
(= (count s)
(count (keep pred? s))))
(define (nothing-but-a-words? s)
(all? a-word s))
- Without keep and count, the only way so
far:
(define (nothing-but-a-words? s)
(cond ((= (count s) 0) #t)
((= (count s) 1) (a-word (first s)))
((= (count s) 2) (and (a-word (first s))
(a-word (first (bf s)))))
((= (count s) 3) (and (a-word (first s))
(a-word (first (bf s)))
(a-word (first (bf (bf s))))))
...etc
(define (nothing-but-a-words? s)
(if (empty? s)
#t
(and (a-word? (first s))
(nothing-but-a-words? (butfirst s))))
Summary
- We saw an introduction to recursion, one of the most powerful
techniques you'll learn in this course!
- We'll be talking about this for the next couple of weeks
Next Time
- We'll look at more and more and more examples of recursion...
Puzzle : The Checkerboard problem
- If two opposite corners of a checkerboard are removed, can
the checkerboard be covered by dominos?
- Assume that each domino is the size of two adjacent squares
of the checkerboard.
- The dominos cannot be placed on top of each other and must
lie flat.
Game : Domineering
- Left and Right, two players, take turns in placing dominoes
on a checker-board.
- Left orients his dominoes North-South and Right East-West.
- Each domino must exactly cover two squares of the board and
no two dominoes may overlap.
- A player who can find no room for one of his dominoes loses.
References
WWW Maven: Dan
Garcia (ddgarcia@cs.berkeley.edu)
Send me feedback