University of California, Berkeley
EECS Department - Computer Science Division

CS3 Lecture 10
Advanced Recursion


Overview of today's lecture


Review

Tail Recursion


Advanced Recursion

Overview

General techniques for solving recursive problems

Pascal's triangle : pascal

  columns (C)

r
o
w
s

(R)
0 1 2 3 4 5 ...
0

1
         

 ...
1

1

1
       

...
2

1

2

1
     

...
3

1

3

3

1
   

...
4

1

4

6

4

1
 

...
5

1

5

10

10

5

1

...
... 

...

...

...

...

...

...

...

Pascal's Triangle

pascal(C,R) = pascal(C-1,R-1) + pascal(C,R-1)

(define (pascal C R)
==>
==>
==>
pascal(C,R) = R! / ((R-C)! C!)

Insertion Sort : sort

(insert 'c '(a b d e)) ==> (a b c d e)
(define (insert w sorted)
==>
==>
==>
==>
==>

(define (sort s)
==>
==>
==>
==>

Pair all prefixes with suffixes : pair-all

(pair-all '(a b c d) '(1 2 3))
;; ==> (a1 a2 a3 b1 b2 b3 c1 c2 c3 d1 d2 d3)

(pair-all '(to re on) '(ad ward ly))
;; ==> (toad toward toly read reward rely onad onward only)
(define (append-prefix prefix suffixes)
==>
==>
==>

(define (pair-all prefixes suffixes)
==>
==>
==>
==>
==>
==>
==>


Summary

Next Time

Puzzle : Two Kids [courtesy Ross Levinsky]

Game : Odd or Even ["Pentagames" by Pentagram, Fireside Publishing, 1990]

| | | | | | | | | | | | | | |