300 likes | 506 Views
Lecture 6: Cons car cdr sdr wdr. David Evans http://www.cs.virginia.edu/~evans. CS200: Computer Science University of Virginia Computer Science. Menu. Recursion Practice: fibo History of Scheme: LISP Introducing Lists. Defining Recursive Procedures. Be optimistic.
E N D
Lecture 6: Cons car cdr sdr wdr David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science
Menu • Recursion Practice: fibo • History of Scheme: LISP • Introducing Lists CS 200 Spring 2004
Defining Recursive Procedures • Be optimistic. • Assume you can solve it. • If you could, how would you solve a bigger problem. • Think of the simplest version of the problem, something you can already solve. (This is the base case.) • Combine them to solve the problem. CS 200 Spring 2004
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 CS 200 Spring 2004
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)? CS 200 Spring 2004
Tracing Fibo > (require-library "trace.ss") > (trace fibo) (fibo) > (fibo 3) |(fibo 3) | (fibo 2) | 1 | (fibo 1) | 1 |2 2 This turns tracing on CS 200 Spring 2004
> (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 (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)? CS 200 Spring 2004
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)) CS 200 Spring 2004
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 CS 200 Spring 2004
;;; 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!! CS 200 Spring 2004
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 CS 200 Spring 2004
LISP “Lots of Insipid Silly Parentheses” “LISt Processing language” Lists are pretty important – hard to write a useful Scheme program without them. CS 200 Spring 2004
Making Lists CS 200 Spring 2004
Making a Pair > (cons 1 2) (1 . 2) 1 2 consconstructs a pair CS 200 Spring 2004
Splitting a Pair cons > (car (cons 1 2)) 1 > (cdr (cons 1 2)) 2 1 2 car cdr car extracts first part of a pair cdr extracts second part of a pair CS 200 Spring 2004
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) CS 200 Spring 2004
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) CS 200 Spring 2004
Pairs are fine, but how do we make threesomes? CS 200 Spring 2004
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? CS 200 Spring 2004
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))) CS 200 Spring 2004
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))) CS 200 Spring 2004
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 …? CS 200 Spring 2004
Lists List ::= (consElementList) 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! CS 200 Spring 2004
From Lecture 5 Recursive Transition Networks ORNATE NOUN end begin NOUN ARTICLE ADJECTIVE 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 CS 200 Spring 2004
Recursive Transition Networks ORNATE NOUN end begin NOUN ARTICLE ADJECTIVE ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::= CS 200 Spring 2004
Lists List ::= (consElementList) List ::= It’s hard to write this! A list is either: a pair where the second part is a list or, empty CS 200 Spring 2004
Null List ::= (consElementList) List ::= null A list is either: a pair where the second part is a list or, empty (null) CS 200 Spring 2004
List Examples > null () > (cons 1 null) (1) > (list? null) #t > (list? (cons 1 2)) #f > (list? (cons 1 null)) #t CS 200 Spring 2004
More List Examples > (list? (cons 1 (cons 2 null))) #t > (car (cons 1 (cons 2 null))) 1 > (cdr (cons 1 (cons 2 null))) (2) CS 200 Spring 2004
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) CS 200 Spring 2004