Class 8 - University of Virginia

advertisement
Lecture 8:
Cons
car
cdr
sdr
wdr
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/~evans
Menu
• History of Scheme
–LISP
• Lists
• List Recursion
3 February 2003
CS 200 Spring 2003
2
Confusion Is
Good!
It means you are learning new
ways of thinking.
3 February 2003
CS 200 Spring 2003
3
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
3 February 2003
CS 200 Spring 2003
4
LISP
“Lots of Insipid Silly Parentheses”
“LISt Processing language”
Lists are pretty important – hard to
write a useful Scheme program
without them.
3 February 2003
CS 200 Spring 2003
5
Making Lists
3 February 2003
CS 200 Spring 2003
6
Making a Pair
> (cons 1 2)
(1 . 2)
1 2
cons constructs a pair
3 February 2003
CS 200 Spring 2003
7
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
3 February 2003
CS 200 Spring 2003
8
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 matters unless you have an IBM 704
• Think of them as first and rest
(define first car)
(define rest cdr)
3 February 2003
CS 200 Spring 2003
9
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)
3 February 2003
CS 200 Spring 2003
10
Pairs are fine, but how do
we make threesomes?
3 February 2003
CS 200 Spring 2003
11
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?
3 February 2003
CS 200 Spring 2003
12
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)))
3 February 2003
CS 200 Spring 2003
13
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)))
3 February 2003
CS 200 Spring 2003
14
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 …?
3 February 2003
CS 200 Spring 2003
15
Lists
List ::= (cons Element List)
A list is a pair where the second part is a list.
One little problem: how do we stop?
This only allows infinitely long lists!
3 February 2003
CS 200 Spring 2003
16
From Lecture 6
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
3 February 2003
CS 200 Spring 2003
17
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN
ADJECTIVES
::= ADJECTIVE ADJECTIVES
ADJECTIVES
::=
3 February 2003
CS 200 Spring 2003
18
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
3 February 2003
CS 200 Spring 2003
19
Null
List ::= (cons Element List)
List ::= null
A list is either:
a pair where the second part is a list
or, empty (null)
3 February 2003
CS 200 Spring 2003
20
List Examples
> null
()
> (cons 1 null)
(1)
> (list? null)
#t
> (list? (cons 1 2))
#f
> (list? (cons 1 null))
#t
3 February 2003
CS 200 Spring 2003
21
More List Examples
> (list? (cons 1 (cons 2 null)))
#t
> (car (cons 1 (cons 2 null)))
1
> (cdr (cons 1 (cons 2 null)))
(2)
3 February 2003
CS 200 Spring 2003
22
List Recursion
3 February 2003
CS 200 Spring 2003
23
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.
3 February 2003
CS 200 Spring 2003
24
Defining Recursive Procedures
on Lists
1. Be optimistic. Be very optimistic
– Assume you can solve it.
– If you could, how would you solve a bigger
For lists, assume we can solve
problem.
it for the cdr
2. Think of the simplest version of the
problem, something you can already
For lists, the simplest version is
solve.
usually null (the zero-length list)
3. Combine them to solve the problem.
3 February 2003
Combine something on the car of the list with
the recursiveCSevaluation
200 Spring 2003 on the cdr. Remember
25
to test null? before using car or cdr.
Defining Sumlist
(define sumlist
(lambda (lst)
(if (null? lst)
> (sumlist (list 1 2 3 4))
10
> (sumlist null)
0
0
( + (car lst) (sumlist (cdr lst))
3 February 2003
CS 200 Spring 2003
26
Defining Productlist
(define productlist
(lambda (lst)
(if (null? lst)
> (productlist (list 1 2 3 4))
24
> (productlist null)
1
1
(*
3 February 2003
(car lst) (sumlist (cdr lst))
CS 200 Spring 2003
27
Defining Length
(define length
(lambda (lst)
(if (null? lst)
> (length (list 1 2 3 4))
4
> (length null)
0
0
( + (car1lst) (length (cdr lst))
3 February 2003
CS 200 Spring 2003
28
Defining insertl
(define insertl
(lambda (lst f stopval)
(if (null? lst)
stopval
(f (car lst)
(insertl (cdr lst) f stopval)))))
3 February 2003
CS 200 Spring 2003
29
Definitions
(define (sumlist lst)
(insertl lst + 0))
(define insertl
(lambda (lst f stopval)
(if (null? lst)
stopval
(f (car lst)
(insertl (cdr lst) f stopval)))))
(define (productlist lst)
(insertl lst * 1))
(define (length lst)
(insertl lst
(lambda (head rest) (+ 1 rest))
0))
3 February 2003
CS 200 Spring 2003
30
Charge
• Next Time: lots more things you can
do with lists (including the peg board
puzzle!)
• PS3 Out Today
– Use lists to make fractals
– You have seen everything you need for it
after today
– Due next week Wednesday
3 February 2003
CS 200 Spring 2003
31
Download