Class 21: Imperative Programming University of Virginia cs1120 David Evans.

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
Class 25: Python, Objects, Bombs, and Inheritance University of Virginia cs1120 David Evans.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Class 7: List Procedures David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
David Evans CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
David Evans CS200: Computer Science University of Virginia Computer Science Class 26: Halting Problem It is plain at any.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Lecture 4: Metacircles Eval Apply David Evans
CS 550 Programming Languages Jeremy Johnson
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
CS 270 Math Foundations of CS Jeremy Johnson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
Mutators for compound data Stack Queue
Dan Grossman / Eric Mullen Autumn 2017
List Allocation and Garbage Collection
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Class 21: Imperative Programming University of Virginia cs1120 David Evans

Menu Why mlength is still running from last class... Aliasing Reversing a list

Appending vs. Appending! 3 (define (list-append p q) (if (null? p) q (cons (car p) (list-append (cdr p) q)))) (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)))) (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  (N p ), N p is number of elements in p Number of new cons cells:  (N p ) Running time in  (N p ), number of elements in p Number of new cons cells: 0

Does it matter? 4 > (define r1 (random-list )) > (define r2 (random-list )) > (time (begin (list-append r1 r2) true)) cpu time: 110 real time: 122 gc time: 78 #t > (define m1 (random-mlist )) > (define m2 (random-mlist )) > (time (begin (mlist-append! m1 m2) true)) cpu time: 15 real time: 22 gc time: 0 #t > (mlength m1) > (time (begin (mlist-append! m1 m2) true)) cpu time: 47 real time: 45 gc time: 0 #t > (mlength m1) (define (random-list n) (if (= n 0) null (cons (random 1000) (random-list (- n 1))))) (define (random-mlist n) (if (= n 0) null (mcons (random 1000) (random-mlist (- n 1)))))

Aliasing Problems 1 1 m1: (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)))) (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) 4 4 m2:

Aliasing Problems 1 1 m1: m2: > (mlist-append! m1 m2) > m1 { } > m2 {4 5 6} > (set-mcar! m2 7) > m2 {7 5 6} > m1 { } > (mlist-append! m1 m2) > m1 { } > m2 {4 5 6} > (set-mcar! m2 7) > m2 {7 5 6} > m1 { }

Aliasing Problems 1 1 m1: m2: > (mlist-append! m1 m2) > (mlist-length m1) > (mlist-append! m1 m2) > (mlist-length m1) (define (mlist-length p) (if (null? p) 0 (+ 1 (mlist-length (mcdr p))))) (define (mlist-length p) (if (null? p) 0 (+ 1 (mlist-length (mcdr p)))))

Reversing 8 (define (list-reverse p) (if (null? p) null (list-append (list-reverse (cdr p)) (list (car p))))) (define (list-reverse p) (if (null? p) null (list-append (list-reverse (cdr p)) (list (car p))))) Running time is in  (N 2 ) where N is number of elements in p. Number of new cons cells: for the list-appends: N-1 + N-2 + … + 1 = N 2 / 2 + for the (list (car p)): N memory use is in  (N 2 ) Define a mlist-reverse! that reverses the elements of a list. The number of cons cells it creates should not scale with the length of the list.

mlist-reverse! 9 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!) Hint: define a helper procedure that takes two inputs.

Revers!ing a List 1 1 m1: revm1:

Charge PS5 Due Monday Read Tyson’s “Science’s Endless Golden Age” (I’ll talk about this next Friday) Prof. Weimer will be here Friday