Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Advertisements

Class 21: Imperative Programming University of Virginia cs1120 David Evans.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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 16: Power Analysis.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
CSE341: Programming Languages Lecture 15 Mutation, Pairs, Thunks, Laziness, Streams, Memoization Dan Grossman Fall 2011.
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 28: Implementing Interpreters.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
Scheme in Scheme 1. 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 18: Think Globally, Mutate Locally.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
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.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
Class 28: Interpreters cs1120 Fall 2009 David Evans.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
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.
Lecture 3: Rules of Evaluation CS150: Computer Science
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 Lecture 8: Cons car cdr sdr wdr.
CS314 – Section 5 Recitation 9
Scheme in Scheme 1.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Edited by Original material by Eric Grimson
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
Class 19: Think Globally, Mutate Locally CS150: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Lecture 8: Recursion Practice CS200: Computer Science
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 28: Types of Types
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Streams, Delayed Evaluation and a Normal Order Interpreter
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Lecture 9: The Great Lambda Tree of Knowledge and Power
Dan Grossman / Eric Mullen Autumn 2017
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

cs1120 Fall 2009 David Evans Lecture 20: Programming with State

2 x : 3 x : 17 y : 3 What would create this environment? From Friday’s class: > (define x 3) > ((lambda (x) … ) 17)

Stateful Application Rule 3 To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment or the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment or the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

4 x : 3 x : 17 y : 3 To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment or the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment or the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To create this environment, a procedure whose environment is the Blue environment was applied. > (define x 3) > ((lambda (x) ((lambda (y) x) 3) 17)

Programming Styles Functional Programming – Program by composing functions – Substitution model applies – Problem Sets 1-4 Imperative Programming – Programming by changing state – Requires stateful model of evaluation – Problem Set 5 and beyond 5 Most programs combine aspects of both styles: even imperative-style programs still involve composing procedures, and very few programs are completely functional.

Mapping Functional Solution: A procedure that takes a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list. (define (list-map f p) (if (null? p) null (cons (f (car p)) (list-map f (cdr p)))))

Imperative Solution A procedure that takes a procedure and a mutable list as arguments, and replaces each element in the list with the value of the procedure applied to that element. It produces no output. (define (mlist-map! f p) (if (null? p) (void) (begin (set-mcar! p (f (mcar p))) (mlist-map! f (mcdr p))))) (define (mlist-map! f p) (if (null? p) (void) (begin (set-mcar! p (f (mcar p))) (mlist-map! f (mcdr p))))) (define (list-map f p) (if (null? p) null (cons (f (car p)) (list-map f (cdr p)))))

Programming with Mutation > (mlist-map! square (mlist )) > (define i4 (mlist )) > (mlist-map! square i4) > i4 ( ) Functional Imperative > (define i4 (intsto 4)) > (map square i4) ( ) > i4 ( )

Comparing Cost 9 (define (list-map f p) (if (null? p) null (cons (f (car p)) (list-map f (cdr p))))) (define (mlist-map! f p) (if (null? p) (void) (begin (set-mcar! p (f (mcar p))) (mlist-map! f (mcdr p))))) (define (mlist-map! f p) (if (null? p) (void) (begin (set-mcar! p (f (mcar p))) (mlist-map! f (mcdr p))))) Functional Imperative Assuming f has constant running time, running time is in  (N) where N is the number of elements in p. Also has running time in  (N) : N recursive calls, constant work each time. Memory use is in  (N) where N is the number of elements: it requires construction N new cons cells. Memory use is in O(1) : no new cons cells are created! (Aside: because it is tail recursive, no evaluation stack either.)

Appending 10 (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 list!”) (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 list!”) (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? 11 > (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)))))

Charge Reading (finish by next Monday): Science’s Endless Golden Age by Neil DeGrasse Tyson PS5 due one week from today 12