Class 22: Stateful Evaluation Rules


Published on

Name, Places, Frames, and Environments
Stateful Evaluation Rules
Exam 1
Tandem Repeats

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Class 22: Stateful Evaluation Rules

  1. 1. Class 22: StatefulEvaluation Rules cs1120 Fall 2011 David Evans 14 October 2011
  2. 2. PlanNames and PlacesEnvironmentsRevised Evaluation RulesExam 1 2
  3. 3. Dennis Ritchie, 1941-20111972 (with Ken Thompson and PDP-11) 3
  4. 4. Fortran LISP (1955, Backus)Programming Languages (1959, McCarthy) Algol Brief History of (1960) Scheme PS1-5 (1975, S&S) C Simula (1969, Ritchie) (1967) PS6-7 C++ (1983, Stroustrup) Python (1991, von Rossum) Java JavaScript PS8 (1995, Sun) (1995, Eich) 4
  5. 5. Why design a newprogramminglanguage? C: wanted both portability and low-level machine access 5
  6. 6. MulticsKen Thompson and Dennis Ritchie Windows and Symbian: not direct descendants of Unix, but they are mostly programmed in C Android iOS Image: cc 6
  7. 7. “Here’s how tosucceed: by beinglucky. Grab on tosomething that’smoving pretty fast.Let yourself becarried on whenyou’re in the rightplace at the righttime.” Dennis Ritchie 7
  8. 8. Recap: Names and PlacesA name refers to a place for storing a value.define creates a new place(set! name expr) changes the value in the place name to the value of exprmcons creates a mutable pair of two new places (set-mcar! pair expr) changes the value in the mcar place of pair to the value of expr (set-mcdr! pair expr) changes the value in the mcdr place of pair to the value of expr 8
  9. 9. Application and Places(lambda (x) …) also creates a new place named x The passed argument is put in that place > (define x 3) x:3 > ((lambda (x) x) 4) x:4 4 >x 3 How are these places different? 9
  10. 10. Location, Location, LocationPlaces live in framesAn environment is a frame and a pointer to a parent environmentAll environments except the global environment have exactly one parent environment, global environment has no parentApplication creates a new environment 10
  11. 11. Environmentsglobal + : #<primitive:+>environment null? : #<primitive:null?> x:3 The global environment points to the outermost frame. It starts with all Scheme built-ins defined. > (define x 3) 11
  12. 12. Stateful Definition Evaluation RuleA definition creates a new place with thedefinition’s name in the frame associated withthe evaluation environment. The value in theplace is value of the definition’s expression.If there is already a place with the name in thecurrent frame, the definition replaces the oldplace with a new place and value. 12
  13. 13. Stateful Name Evaluation RuleTo evaluate a name expression, search theevaluation environment’s frame for a place with aname that matches the name in the expression.If such a place exists, the value of the nameexpression is the value in that place.Otherwise, the value of the name expression is theresult of evaluating the name expression in theparent environment. If the evaluation environmenthas no parent, the name is not defined and thename expression evaluates to an error. 13
  14. 14. Evaluating NamesTo evaluate a name expression,search the evaluation globalenvironment’s frame for a place env x:3with a name that matches thename in the expression.If such a place exists, the value ofthe name expression is the valuein that place. x : 17Otherwise, the value of the nameexpression is the result ofevaluating the name expressionin the parent environment. If theevaluation environment has noparent, the name is not defined y:3and the name expressionevaluates to an error. How are environments like this created? 14
  15. 15. Proceduresglobal + : #<primitive:+>environment null? : #<primitive:null?> x:3 double: ?? > (define double (lambda (x) (+ x x))) 15
  16. 16. How to Draw a ProcedureA procedure needs both code and an environment We’ll see why soonWe draw procedures like this: Environment pointer environment: parameters: x body: (+ x x) 16
  17. 17. How to Draw a Procedure (for artists only) Environment pointer xInput parameters (+ x x)(in mouth) Procedure Body 17
  18. 18. Proceduresglobal + : #<primitive:+>environment null? : #<primitive:null?> x:3 double:> (define double environment: parameters: x body: (+ x x) (lambda (x) (+ x x))) 18
  19. 19. ApplicationOld rule: (Substitution model) Apply Rule 2: Constructed Procedures. To apply a constructed procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. 19
  20. 20. Stateful Application Rule (Constructed Procedures)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. 20
  21. 21. 1. Construct a new global environment environment, parent is procedure’s environment + : #<primitive:+> pointer2. Make places in that frame x:3 double: with the names of each parameter, and operand values environment:3. Evaluate the body in the parameters: x new environment body: (+ x x) x: 4 > (double 4) (+ x x) 8 21
  22. 22. x:3What wouldcreate thisenvironment? x : 17 y:3Think about this, we’lldiscuss it next week… 22
  23. 23. Exam 1 23
  24. 24. Overall Results5 below 70 5 above 100Come to my officehours or arrange ameeting to go Solutions/comments will be posted onover things you course site later by tomorrow.didn’t understand 24
  25. 25. Honor15. Do you trust your classmates to follow the honor expectations in this class?— Yes, I trust them completely.— I worry that there may be a few transgressions, but I believe the vast majority of the class is honorable and it is fair and beneficial to rely on this.— I think this class places too high a burden on students’ honor, and there are enough dishonorable students that it is unfair on the honorable students.— I have reason to suspect that other students violated the honor policy on0 problem sets.— I have direct knowledge of other students violating the honor policy on0 problem sets.— I have reason to suspect that other students violated the honor policy on this0 exam.0— I have direct knowledge of other students violating the honor policy on this exam. 25
  26. 26. Honor ResponsesTrust CompletelyBetween 1 and 2 Vast Majority Too High 0 5 10 15 20 25 30 35 26
  27. 27. Problem 11Define a procedure, count-tandem-repeats, that takesas input a list, p, and a number, n. The output should bethe total number of times the first n elements of p areconsecutively repeated at the beginning of p.Repetitions may not overlap. 27
  28. 28. 28
  29. 29. (define (count-tandem-repeats p n) (count-prefix-repeats p (list-prefix p n))) 29
  30. 30. (define (list-prefix p n) (if (= n 0) null (cons (car p) (list-prefix (cdr p) (- n 1))))) What is the asymptotic running time of list-prefix? 30
  31. 31. (define (contains-matching-prefix p q) ; question 10 (or (null? q) (and (eq? (car p) (car q)) (contains-matching-prefix (cdr p) (cdr q)))))(define (list-prefix p n) (if (= n 0) null (cons (car p) (list-prefix (cdr p) (- n 1)))))(define (count-prefix-repeats p q) (if (contains-matching-prefix p q) (+ 1 (count-prefix-repeats ((n-times cdr (length q)) p) q)) 0))(define (count-tandem-repeats p n) (count-prefix-repeats p (list-prefix p n))) Not defensive: errors if (< (length p) n) 31
  32. 32. Returning Exam 1 Code from Chapter 8(define (list-quicksort cf p) (if (null? p) null (list-append (list-quicksort cf (list-filter (lambda (el) (cf el (car p))) (cdr p))) (cons (car p) (list-quicksort cf (list-filter (lambda (el) (not (cf el (car p)))) (cdr p))))))) (list-quicksort (lambda (s1 s2) (< (get-email-id s1) (get-email-id s2))) cs1120-students) 32