David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

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.
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 Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
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.
1 Lecture 14: Assignment and the Environment Model (EM)
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Lecture 3: Rules of Evaluation CS150: Computer Science
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 27: Types of Types “It would appear.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Edited by Original material by Eric Grimson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
The interpreter.
Lecture 7: List Recursion CS200: Computer Science
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
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 28: Types of Types
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
6.001 SICP Environment model
Lecture 24: Metalinguistics CS200: Computer Science
The Metacircular Evaluator (Continued)
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
Lecture 9: The Great Lambda Tree of Knowledge and Power
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Lecture 11: Sorting Grounds and Bubbles
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments

27 February 2002CS 200 Spring Menu Exam 1 Environments Evaluation Rules

27 February 2002CS 200 Spring How does Exam 1 compare to other CS classes?

27 February 2002CS 200 Spring CS 101 (Spring 2001) 7. Why do programmers comment? (a) Give compiler extra instructions (b) Aid readability (c) To hide code for debugging purposes (d) (b) and (c) (e) none of the above “Correct” answer

27 February 2002CS 200 Spring Why Comment? To assign credit, blame or (non) liability (your name, copyright, disclaimer) To document external assumptions To remind you what you are doing (e.g., ;;; This is a bug!) To keep code readers entertained with jokes, poems, and stories To make sure your employer has to rehire you to fix the code (comments in foreign languages are especially effective)

27 February 2002CS 200 Spring CS 201 (Fall 2001) 5. [13 points total] Recursion. The “SuperCool” function is defined for positive integers greater than or equal to zero as: supercool(i) = supercool(i-1) * supercool(i-2); supercool(0) = 1; supercool(1) = 2; For example: supercool(2) = 2 * 1 = 2 supercool(3) = 2 * 2 = 4 supercool(4) = 4 * 2 = 8 supercool(5) = 8 * 4 = 32 Write a recursive function that calculates supercool. Your function must be recursive to receive any credit.

27 February 2002CS 200 Spring CS 415 (Spring 2002) 3. Define a function (zip op x y) that takes lists (x1 x2 x3....) and (y1 y2 y3...) and evaluated to the list ((op x1 y1) (op x2 y2) (op x3 y3)...). Examples: > (zip + '( ) '( )) ( ) > (zip list '(a b c d) '( )) ((a 6) (b 7) (c 8) (d 9))

27 February 2002CS 200 Spring zip (define (zip f list1 list2) (if (null? list1) null (cons (f (car list1) (car list2)) (zip f (cdr list1) (cdr list2))))) (define zip map) (Easy way)

27 February 2002CS 200 Spring CS 655 (Spring 2001) 1. Mergering a. Write a Scheme procedure listadder that combines two lists by adding their elements. For example, (listadder '(3 1 3) '(3 4 2)) should produce (6 5 5). b. Write deeplistadder that works on nested lists also. For example, (deeplistadder '((1 2 (3 4)) 4) '((1 2 (3 4)) 6)) should produce ((2 4 (6 8)) 10).

27 February 2002CS 200 Spring listadder (define (listadder list1 list2) (if (null? list1) '() (cons (+ (car list1) (car list2)) (listadder (cdr list1) (cdr list2)))))

27 February 2002CS 200 Spring deeplistadder (define (deeplistadder list1 list2) (if (null? list1) list1 (if (list? (car list1)) (cons (deeplistadder (car list1) (car list2)) (deeplistadder (cdr list1) (cdr list2))) (cons (+ (car list1) (car list2)) (deeplistadder (cdr list1) (cdr list2))))))

27 February 2002CS 200 Spring deeplistmerger (define (deeplistmerger f list1 list2) (if (null? list1) list1 (if (list? (car list1)) (cons (deeplistmapper f (car list1) (car list2)) (deeplistmapper f (cdr list1) (cdr list2))) (cons (f (car list1) (car list2)) (deeplistmapper f (cdr list1) (cdr list2))))))

27 February 2002CS 200 Spring PhD Qualifying Exam Using any language you want, write a program that reverses a list?

27 February 2002CS 200 Spring reverse A recursive definition: –Reversing an empty list is an empty list –To reverse a non-empty list, put the first element of the list at the end of the result of reversing the rest of the list (a. rest)  (reverse rest). a  (reverse 2 3 4) 1  (reverse 3 4) 2 1  (reverse 4)  (reverse ) 

27 February 2002CS 200 Spring reverse try 1: (define (reverse lst) (if (null? lst) lst (cons (reverse (cdr lst)) (car lst)))) (reverse ‘(1 2)) ((lambda (lst) (if (null? lst) lst (cons (reverse (cdr lst)) (car lst)))) ‘(1 2)) (if (null? ‘(1 2)) ‘(1 2) (cons (reverse (cdr ‘(1 2))) (car ‘(1 2))))) (cons (reverse ‘(2)) 1) (cons 2 1) (2. 1) Not the list (2 1)

27 February 2002CS 200 Spring reverse (define (reverse lst) (if (null? lst) lst (append (reverse (cdr lst)) (list (car lst))))) (n)(n) n times reverse is  (n 2 ) Challenge: Define a reverse procedure that is  (n) without using mutation

27 February 2002CS 200 Spring Environments

27 February 2002CS 200 Spring (define nest (lambda (x) (+ x x)))) > ((nest 3) 4) 8 Does the substitution model of evaluation tell us how to evaluate this?

27 February 2002CS 200 Spring Review: Names, Places, Mutation A name is a place for storing a value. define creates a new place cons creates two new places, the car and the cdr (set! name expr) changes the value in the place name to the value of expr (set-car! pair expr) changes the value in the car place of pair to the value of expr (set-cdr! pair expr) changes the value in the cdr place of pair to the value of expr

27 February 2002CS 200 Spring Lambda and Places (lambda (x) …) also creates a new place named x The passed argument is put in that place > (define x 3) > ((lambda (x) x) 4) 4 > x 3 How are these places different? x : 3 x : 4

27 February 2002CS 200 Spring Location, Location, Location Places live in frames An environment is a pointer to a frame We start in the global environment Application creates a new frame All frames except the global frame have one parent frame

27 February 2002CS 200 Spring Environments global environment > (define x 3) + : # null? : # The global environment points to the outermost frame. It starts with all Scheme primitives. x : 3

27 February 2002CS 200 Spring Procedures global environment > (define double (lambda (x) (+ x x))) + : # null? : # double: ?? x : 3

27 February 2002CS 200 Spring How to Draw a Procedure A procedure needs both code and an environment –We’ll see why soon We draw procedures like this: Environment pointer Code pointer parameters: x body: (+ x x)

27 February 2002CS 200 Spring Procedures global environment > (define double (lambda (x) (+ x x))) + : # null? : # double: x : 3 parameters: x body: (+ x x)

27 February 2002CS 200 Spring Application Old rule: (Substitution model) Apply Rule 2: Compounds. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value.

27 February 2002CS 200 Spring Application 1.Construct a new frame, enclosed in the environment of this procedure 2.Make places in that frame with the names of each parameter 3.Put the values of the parameters in those places 4.Evaluate the body in the new environment

27 February 2002CS 200 Spring Construct a new frame, enclosed in the environment of this procedure 2.Make places in that frame with the names of each parameter 3.Put the values of the parameters in those places 4.Evaluate the body in the new environment global environment > (double 4) 8 + : # double: x : 3 parameters: x body: (+ x x) x : 4 (+ x x) (+ 4 4) 8

global environment (nest 3) + : # nest: x : 3 parameters: x body: (lambda (x) (+ x x)) x : 3 (define nest (lambda (x) (+ x x)))) > ((nest 3) 4) ((lambda (x) (+ x x)) 4) x : 4 (+ x x)

27 February 2002CS 200 Spring Evaluation Rule 2 (Names) If the expression is a name, it evaluates to the value associated with that name. To find the value associated with a name, look for the name in the frame pointed to by the evaluation environment. If it contains a place with that name, use the value in that place. If it doesn’t, evaluate the name using the frame’s parent environment as the new evaluation environment. If the frame has no parent, error (name is not a place).

27 February 2002CS 200 Spring evaluate-name (define (evaluate-name name env) (if (null? env) (error “Undefined name: …”) (if (frame-contains name (get-frame env)) (lookup name (get-frame env)) (evaluate-name name (parent-environment (get-frame env)))))) Hmm…maybe we can define a Scheme interpreter in Scheme!

27 February 2002CS 200 Spring Charge Mutation makes evaluation rules more complicated Environment diagrams quickly get complicated – but like substitution evaluations, just follow rules mechanically Read SICP Ch 3 (through p. 266) Get cracking on PS5 –Lab hours – Monday 8-9:30 After Spring Break – we will define our own Scheme interpreter!