Download presentation

Presentation is loading. Please wait.

Published byNaomi Bevans Modified over 3 years ago

1
David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr

2
28 January 2004CS 200 Spring 20042 Menu Recursion Practice: fibo History of Scheme: LISP Introducing Lists

3
28 January 2004CS 200 Spring 20043 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.

4
28 January 2004CS 200 Spring 20044 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

5
28 January 2004CS 200 Spring 20045 Fibo Results > (fibo 2) 1 > (fibo 3) 2 > (fibo 4) 3 > (fibo 10) 55 > (fibo 100) Still working after 4 hours… Why cant our 100,000x Apollo Guidance Computer calculate (fibo 100) ?

6
28 January 2004CS 200 Spring 20046 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

7
28 January 2004CS 200 Spring 20047 > (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) ?

8
28 January 2004CS 200 Spring 20048 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))

9
28 January 2004CS 200 Spring 20049 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

10
28 January 2004CS 200 Spring 200410 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.916666666666666 According to Fibonaccis model, after less than 10 years, rabbits would out-weigh the Earth!

11
28 January 2004CS 200 Spring 200411 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

12
28 January 2004CS 200 Spring 200412 LISP Lots of Insipid Silly Parentheses LISt Processing language Lists are pretty important – hard to write a useful Scheme program without them.

13
28 January 2004CS 200 Spring 200413 Making Lists

14
28 January 2004CS 200 Spring 200414 Making a Pair > (cons 1 2) (1. 2) 1 2 cons constructs a pair

15
28 January 2004CS 200 Spring 200415 Splitting a Pair > (car (cons 1 2)) 1 > (cdr (cons 1 2)) 2 1 2 cons car cdr car extracts first part of a pair cdr extracts second part of a pair

16
28 January 2004CS 200 Spring 200416 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) Doesnt 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)

17
28 January 2004CS 200 Spring 200417 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)

18
28 January 2004CS 200 Spring 200418 Pairs are fine, but how do we make threesomes?

19
28 January 2004CS 200 Spring 200419 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?

20
28 January 2004CS 200 Spring 200420 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)))

21
28 January 2004CS 200 Spring 200421 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)))

22
28 January 2004CS 200 Spring 200422 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 …?

23
28 January 2004CS 200 Spring 200423 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!

24
28 January 2004CS 200 Spring 200424 Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN 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 From Lecture 5

25
28 January 2004CS 200 Spring 200425 Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::=

26
28 January 2004CS 200 Spring 200426 Lists List ::= (cons Element List ) List ::= A list is either: a pair where the second part is a list or, empty Its hard to write this!

27
28 January 2004CS 200 Spring 200427 Null List ::= (cons Element List ) List ::= A list is either: a pair where the second part is a list or, empty ( null ) null

28
28 January 2004CS 200 Spring 200428 List Examples > null () > (cons 1 null) (1) > (list? null) #t > (list? (cons 1 2)) #f > (list? (cons 1 null)) #t

29
28 January 2004CS 200 Spring 200429 More List Examples > (list? (cons 1 (cons 2 null))) #t > (car (cons 1 (cons 2 null))) 1 > (cdr (cons 1 (cons 2 null))) (2)

30
28 January 2004CS 200 Spring 200430 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)

Similar presentations

OK

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Free ppt on dispersion of light Option call put ppt on loop Ppt on product specification example Ppt on vehicle testing Ppt on statistics in maths games Ppt on media revolution 7 Ppt on information technology companies Download ppt on folk dances of india Ppt on computer application in daily life Ppt on food trucks