Lecture 6: Cons car cdr sdr wdr CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans Menu • Recursion Practice: fibo • History of Scheme: LISP • Introducing Lists 28 January 2004 CS.

Download Report

Transcript Lecture 6: Cons car cdr sdr wdr CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans Menu • Recursion Practice: fibo • History of Scheme: LISP • Introducing Lists 28 January 2004 CS.

Lecture 6:
Cons
car
cdr
sdr
wdr
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/~evans
Menu
• Recursion Practice: fibo
• History of Scheme: LISP
• Introducing Lists
28 January 2004
CS 200 Spring 2004
2
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
3
Defining fibo
;;; (fibo n) evaluates to the nth Fibonacci
;;; number
(define (fibo n)
FIBO (1) = FIBO (2) = 1
(if (or (= n 1) (= n 2))
1 ;;; base case
FIBO (n) =
FIBO (n – 1)
(+ (fibo (- n 1))
+ FIBO (n – 2)
(fibo (- n 2)))))
for n > 2
28 January 2004
CS 200 Spring 2004
4
Fibo Results
> (fibo 2)
1
Why
can’t
our
> (fibo 3)
100,000x Apollo
2
Guidance
> (fibo 4)
3
Computer calculate
> (fibo 10)
(fibo 100)?
55
> (fibo 100)
Still working after 4 hours…
28 January 2004
CS 200 Spring 2004
5
Tracing Fibo
> (require-library "trace.ss")
> (trace fibo)
(fibo)
> (fibo 3)
|(fibo 3)
|
(fibo 2)
|
1
|
(fibo 1)
|
1
|2
2
28 January 2004
CS 200 Spring 2004
This turns tracing on
6
> (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
|5
5
28 January 2004
(fibo 5) = (fibo 4)
(fibo 3) +
(fibo 2)
(fibo 2) + (fibo 1) +
1
1
+ 1
2
+
1
3
= 5
+ (fibo 3)
+ (fibo 2) + (fibo 1)
+ 1
+ 1
+ 2
+ 2
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)?
CS 200 Spring 2004
7
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
8
Fast-Fibo Results
> (fast-fibo 1)
1
> (fast-fibo 10)
55
> (time (fast-fibo 100))
cpu time: 0 real time: 0 gc time: 0
354224848179261915075
28 January 2004
CS 200 Spring 2004
9
;;; 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.916666666666666
According to Fibonacci’s model, after less than 10
years, rabbits would out-weigh the Earth!
Beware the Bunnies!!
28 January 2004
CS 200 Spring 2004
10
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
11
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
12
Making Lists
28 January 2004
CS 200 Spring 2004
13
Making a Pair
> (cons 1 2)
(1 . 2)
1 2
cons constructs a pair
28 January 2004
CS 200 Spring 2004
14
Splitting a Pair
> (car (cons 1 2))
1
> (cdr (cons 1 2))
2
cons
1 2
car
cdr
car extracts first part of a pair
cdr extracts second part of a pair
28 January 2004
CS 200 Spring 2004
15
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)
28 January 2004
(The DrScheme “Pretty Big” language
already defines these, but they are not part
of standard Scheme)
CS 200 Spring 2004
16
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
17
Pairs are fine, but how do
we make threesomes?
28 January 2004
CS 200 Spring 2004
18
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
19
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
20
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
21
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
22
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
23
From Lecture 5
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN
28 January 2004
CS 200 Spring 2004
24
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN
ADJECTIVES
::= ADJECTIVE ADJECTIVES
ADJECTIVES
::=
28 January 2004
CS 200 Spring 2004
25
Lists
List ::= (cons Element List)
List ::=
It’s hard to write this!
A list is either:
a pair where the second part is a list
or, empty
28 January 2004
CS 200 Spring 2004
26
Null
List ::= (cons Element List)
List ::= null
A list is either:
a pair where the second part is a list
or, empty (null)
28 January 2004
CS 200 Spring 2004
27
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
28
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
29
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)
28 January 2004
CS 200 Spring 2004
30