David Evanshttp://www.cs.virginia.edu/~evans
CS200: Computer ScienceUniversity of VirginiaComputer Science
Lecture 6: Conscar cdr sdr wdr
28 January 2004 CS 200 Spring 2004 2
Menu
• Recursion Practice: fibo
• History of Scheme: LISP
• Introducing Lists
28 January 2004 CS 200 Spring 2004 3
Defining Recursive Procedures
1. Be optimistic.– Assume you can solve it.– If you could, how would you solve a bigger
problem.
2. Think of the simplest version of the problem, something you can already solve. (This is the base case.)
3. Combine them to solve the problem.
28 January 2004 CS 200 Spring 2004 4
Defining fibo
;;; (fibo n) evaluates to the nth Fibonacci
;;; number
(define (fibo n)
(if (or (= n 1) (= n 2))
1 ;;; base case
(+ (fibo (- n 1))
(fibo (- n 2)))))
FIBO (1) = FIBO (2) = 1
FIBO (n) = FIBO (n – 1) + FIBO (n – 2)
for n > 2
28 January 2004 CS 200 Spring 2004 5
Fibo Results> (fibo 2)1> (fibo 3)2> (fibo 4)3> (fibo 10)55> (fibo 100)Still working after 4 hours…
Why can’t our 100,000x Apollo Guidance Computer calculate (fibo 100)?
28 January 2004 CS 200 Spring 2004 6
Tracing Fibo> (require-library "trace.ss")> (trace fibo)(fibo)> (fibo 3)|(fibo 3)| (fibo 2)| 1| (fibo 1)| 1|22
This turns tracing on
28 January 2004 CS 200 Spring 2004 7
> (fibo 5)|(fibo 5)| (fibo 4)| |(fibo 3)| | (fibo 2)| | 1| | (fibo 1)| | 1| |2| |(fibo 2)| |1| 3| (fibo 3)| |(fibo 2)| |1| |(fibo 1)| |1| 2|55
(fibo 5) = (fibo 4) + (fibo 3) (fibo 3) + (fibo 2) + (fibo 2) + (fibo 1) (fibo 2) + (fibo 1) + 1 + 1 + 1 1 + 1 2 + 1 + 2 3 + 2 = 5
To calculate (fibo 5) we calculated:(fibo 4) 1 time(fibo 3) 2 times(fibo 2) 3 times(fibo 1) 2 times = 8 calls to fibo = (fibo 6)
How many calls to calculate (fibo 100)?
28 January 2004 CS 200 Spring 2004 8
fast-fibo
(define (fast-fibo n) (define (fibo-worker a b count) (if (= count 1) b (fibo-worker (+ a b) a (- count
1)))) (fibo-worker 1 1 n))
28 January 2004 CS 200 Spring 2004 9
Fast-Fibo Results
> (fast-fibo 1)1> (fast-fibo 10)55> (time (fast-fibo 100))cpu time: 0 real time: 0 gc time: 0354224848179261915075
28 January 2004 CS 200 Spring 2004 10
Beware the Bunnies!!
;;; The Earth's mass is 6.0 x 10^24 kg> (define mass-of-earth (* 6 (expt 10 24))) ;;; A typical rabbit's mass is 2.5 kilograms> (define mass-of-rabbit 2.5)> (/ (* mass-of-rabbit (fast-fibo 100)) mass-of-earth)0.00014759368674135913> (/ (* mass-of-rabbit (fast-fibo 110)) mass-of-earth)0.018152823441189517> (/ (* mass-of-rabbit (fast-fibo 119)) mass-of-earth)1.379853393132076> (exact->inexact (/ 119 12))9.916666666666666According to Fibonacci’s model, after less than 10 years, rabbits would out-weigh the Earth!
28 January 2004 CS 200 Spring 2004 11
History of Scheme• Scheme [1975]
– Guy Steele and Gerry Sussman
– Originally “Schemer”
– “Conniver” [1973] and “Planner” [1967]
• Based on LISP – John McCarthy (late 1950s)
• Based on Lambda Calculus– Alonzo Church (1930s)
– Last few lectures in course
28 January 2004 CS 200 Spring 2004 12
LISP
“Lots of Insipid Silly Parentheses”
“LISt Processing language”
Lists are pretty important – hard to write a useful Scheme program without them.
28 January 2004 CS 200 Spring 2004 13
Making Lists
28 January 2004 CS 200 Spring 2004 14
Making a Pair
> (cons 1 2)(1 . 2) 1 2
cons constructs a pair
28 January 2004 CS 200 Spring 2004 15
Splitting a Pair> (car (cons 1 2))1> (cdr (cons 1 2))2
1 2
cons
car cdr
car extracts first part of a paircdr extracts second part of a pair
28 January 2004 CS 200 Spring 2004 16
Why “car” and “cdr”?• Original (1950s) LISP on IBM 704
– Stored cons pairs in memory registers– car = “Contents of the Address part of the
Register”– cdr = “Contents of the Decrement part of the
Register” (“could-er”)
• Doesn’t matter unless you have an IBM 704
• Think of them as first and rest(define first car)(define rest cdr)
(The DrScheme “Pretty Big” language already defines these, but they are not part of standard Scheme)
28 January 2004 CS 200 Spring 2004 17
Implementing cons, car and cdr
Using PS2: (define cons make-point)(define car x-of-point)(define cdr y-of-point)
As we implemented make-point, etc.:(define (cons a b) (lambda (w) (if (w) a
b)))(define (car pair) (pair #t)(define (cdr pair) (pair #f)
28 January 2004 CS 200 Spring 2004 18
Pairs are fine, but how do we make threesomes?
28 January 2004 CS 200 Spring 2004 19
Threesome?
(define (threesome a b c) (lambda (w) (if (= w 0) a (if (= w 1) b c))))
(define (first t) (t 0))(define (second t) (t 1))(define (third t) (t 2))Is there a better way of thinking about our triple?
28 January 2004 CS 200 Spring 2004 20
Triple
A triple is just a pair where one of the parts is a pair!
(define (triple a b c) (cons a (cons b c)))(define (t-first t) (car t))(define (t-second t) (car (cdr t))) (define (t-third t) (cdr (cdr t)))
28 January 2004 CS 200 Spring 2004 21
Quadruple
A quadruple is a pair where the second part is a triple
(define (quadruple a b c d) (cons a (triple b c d)))(define (q-first q) (car q))(define (q-second q) (t-first (cdr t))) (define (q-third t) (t-second (cdr t)))(define (q-fourth t) (t-third (cdr t)))
28 January 2004 CS 200 Spring 2004 22
Multuples• A quintuple is a pair where the second part is
a quadruple
• A sextuple is a pair where the second part is a quintuple
• A septuple is a pair where the second part is a sextuple
• An octuple is group of octupi
• A list (any length tuple) is a pair where the second part is a …?
28 January 2004 CS 200 Spring 2004 23
Lists
List ::= (cons Element List)
A list is a pair where the second part is a list.
One big problem: how do we stop?This only allows infinitely long lists!
28 January 2004 CS 200 Spring 2004 24
Recursive Transition Networks
ARTICLE ADJECTIVE NOUN endbegin
ORNATE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE NOUNORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN
From Lecture 5
28 January 2004 CS 200 Spring 2004 25
Recursive Transition Networks
ARTICLE ADJECTIVE NOUN endbegin
ORNATE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN
ADJECTIVES ::= ADJECTIVE ADJECTIVES
ADJECTIVES ::=
28 January 2004 CS 200 Spring 2004 26
Lists
List ::= (cons Element List)List ::=
A list is either: a pair where the second part is a listor, empty
It’s hard to write this!
28 January 2004 CS 200 Spring 2004 27
Null
List ::= (cons Element List)List ::=
A list is either: a pair where the second part is a listor, empty (null)
null
28 January 2004 CS 200 Spring 2004 28
List Examples> null()> (cons 1 null)(1)> (list? null)#t> (list? (cons 1 2))#f> (list? (cons 1 null))#t
28 January 2004 CS 200 Spring 2004 29
More List Examples
> (list? (cons 1 (cons 2 null)))#t> (car (cons 1 (cons 2 null)))1> (cdr (cons 1 (cons 2 null)))(2)
28 January 2004 CS 200 Spring 2004 30
Charge
• Next Time: List Recursion
• PS2 Due Monday– Lots of the code we provided uses lists
– But, you are not expected to use lists in your code (but you can if you want)