class 24: imperative programming

18
Class 24: Imperativ e Programmi ng cs1120 Fall 2011 David Evans 19 October 2011

Upload: david-evans

Post on 20-Nov-2014

448 views

Category:

Technology


1 download

DESCRIPTION

list-append vs. mlist-append!Aliasing

TRANSCRIPT

Page 1: Class 24: Imperative Programming

Class 24: Imperative Programming

cs1120 Fall 2011David Evans19 October 2011

Page 2: Class 24: Imperative Programming

Running time practice throughout!

Menu

Recap: list-append vs. mlist-append!Aliasingmlist-reverse!

I haven’t yet written solutions to the Chapter 9 Exercises, but I will do it soon if at least 3 people provide some evidence that they are trying to solve them! You can do this by posting your solutions or questions as comments on today’s class. I also how to see more comments/suggestions for the ElevenLearning version of the book (post here: http://www.cs.virginia.edu/cs1120/all/2011/on-line-version-of-course-book). If you do at least one of these, you can turn in PS5 at Monday’s class with no penalty.

Page 3: Class 24: Imperative Programming

3

Appending vs. Appending!(define (list-append p q) (if (null? p) q (cons (car p) (list-append (cdr p) q))))

(define (mlist-append! p q) (if (null? p) (if (null? (mcdr p)) (mlist-append! (mcdr p) q))))

Running time:

Memory use:

Page 4: Class 24: Imperative Programming

4

Appending vs. Appending!(define (list-append p q) (if (null? p) q (cons (car p) (list-append (cdr p) q))))

(define (mlist-append! p q) (if (null? p) (error “Cannot append to empty!”) (if (null? (mcdr p)) (set-mcdr! p q) (mlist-append! (mcdr p) q))))

Running time in (Np), Np is number of elements in p

Number of new cons cells: (Np)

Running time in (Np), number of elements in p

Number of new cons cells: 0

Page 5: Class 24: Imperative Programming

5

Does it matter?To test: Define a procedure to make a list of length n.

Page 6: Class 24: Imperative Programming

6

> (define p1 (make-list 100000))> (define p2 (make-list 100000))> (time (begin (list-append p1 p2) (void)))cpu time: 78 real time: 77 gc time: 47> (length p1)100000> (define m1 (make-mlist 100000))> (define m2 (make-mlist 100000))> (time (mlist-append! m1 m2))cpu time: 0 real time: 13 gc time: 0> (mlength m1)200000> (time (mlist-append! m1 m2))cpu time: 31 real time: 26 gc time: 0> (time (mlist-append! m1 m2))

(define (make-list n) (if (= n 0) null (cons 0 (make-list (- n 1)))))

(define (make-mlist n) (if (= n 0) null (mcons 0 (make-mlist (- n 1)))))

> (time (begin (list-append p1 p2) (void)))cpu time: 78 real time: 77 gc time: 47> (time (begin (list-append p1 p2) (void)))cpu time: 78 real time: 83 gc time: 31> (time (begin (list-append p1 p2) (void)))cpu time: 78 real time: 73 gc time: 31> (time (begin (list-append p1 p2) (void)))cpu time: 93 real time: 90 gc time: 31

Page 7: Class 24: Imperative Programming

7

What about the built-in append?

> (time (begin (append p1 p2) (void)))cpu time: 0 real time: 1 gc time: 0> (time (begin (append p1 p2) (void)))cpu time: 0 real time: 2 gc time: 0> (define p200k (append p1 p2))> (define p400k (append p200k p200k))> (define p800k (append p400k p400k))> (define p1600k (append p800k p800k))> (length p1600k)1600000> (time (begin (append p400k p400k) (void)))cpu time: 0 real time: 6 gc time: 0> (time (begin (append p800k p800k) (void)))cpu time: 109 real time: 110 gc time: 94> (time (begin (append p1600k p1600k) (void)))cpu time: 1045 real time: 1044 gc time: 999> (time (begin (append p1600k p1600k) (void)))cpu time: 171 real time: 160 gc time: 125> (time (begin (append p1600k p1600k) (void)))cpu time: 203 real time: 197 gc time: 171

Possible reason built-in append is so much faster:Internal list representation doesn’t have to be just cons pairs (could jump quickly over many cells) Because lists are immutable, doesn’t have to copy list!

Page 8: Class 24: Imperative Programming

8

What about the built-in mappend!

> (require racket/mpair)> (time (begin (mappend! m1 m2) (void)))cpu time: 0 real time: 1 gc time: 0> (time (begin (mappend! m1 m2) (void)))cpu time: 0 real time: 1 gc time: 0> (time (begin (mappend! m1 m2) (void)))

Page 9: Class 24: Imperative Programming

9

Still waiting…Favorite response to Exam Question 14: What topics to you hope to see in the remainder of the

course?:

“I can’t even begin to anticipate what will come next – most of what we’ve done so far has already blown my mind.”

Page 10: Class 24: Imperative Programming

10

Page 11: Class 24: Imperative Programming

Why We’re Still Waiting / Blowing our Minds?

1

m1:

2 3

(define (mlist-append! p q) (if (null? p) (error “Cannot append to empty!”) (if (null? (mcdr p)) (set-mcdr! p q) (mlist-append! (mcdr p) q))))

(mlist-append! m1 m2)

4m2: 5 6

(mlist-append! m1 m2)(mlist-append! m1 m2)

Page 12: Class 24: Imperative Programming

Aliasing

1

m1:

2 3

4m2: 5 67

> (mlist-append! m1 m2)> m1{1 2 3 4 5 6}> m2{4 5 6}> (set-mcar! m2 7)> m2{7 5 6}> m1{1 2 3 7 5 6}

Page 13: Class 24: Imperative Programming

Aliasing Problems

1

m1:

2 3

4m2: 5 6

> (mlist-append! m1 m2)> (mlist-append! m1 m2)> (mlist-length m1) (define (mlist-length p)

(if (null? p) 0 (+ 1 (mlist-length (mcdr p)))))

Page 14: Class 24: Imperative Programming

14

Reversing

Page 15: Class 24: Imperative Programming

15

Analyzing list-reverse(define (list-reverse p) (if (null? p) null (list-append (list-reverse (cdr p)) (list (car p)))))

Page 16: Class 24: Imperative Programming

16

Reversing(define (list-reverse p) (if (null? p) null (list-append (list-reverse (cdr p)) (list (car p)))))

Running time is in (N2) where N is number of elements in p.

Number of new cons cells: for the list-appends: N-1 + N-2 + … + 1 = N2/ 2 + for the (list (car p)): N memory use is in (N2)

Page 17: Class 24: Imperative Programming

17

mlist-reverse!Define a mlist-reverse! that reverses the elements of a mutable list. The output should be a mutable list with the elements in the reverse order. The number of cons cells it creates should not scale with the length of the list. (The input list can be mutated arbitrarily!)

Page 18: Class 24: Imperative Programming

Charge

PS5 Due Friday Except as noted earlier: if you post a comment/suggestion about the ElevenLearning book or a comment that suggests you are trying the Chapter 9 book exercises then it is due Monday