# Not really what happens in the computer. The Environment Model. (set-car! l 'z) (car l) z

Size: px
Start display at page:

Download "Not really what happens in the computer. The Environment Model. (set-car! l 'z) (car l) z"

Transcription

1 6.001 SICP Environment model Models of computation Substitution model A way to figure out what happens during evaluation (define l '(a b c)) (car l) a (define m '(1 2 3)) (car l) a Not really what happens in the computer (car l) a (set-car! l 'z) (car l) z The Environment Model 1

2 Can you figure out why this code works? (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 ; not functional programming! (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 ; ca and cb are independent 2

3 What the EM is: A precise, completely mechanical description of: name-rule define-rule set!-rule lambda-rule application looking up the value of a variable creating a new definition of a var changing the value of a variable creating a procedure applying a procedure Enables analyzing more complex scheme code: Example: make-counter Basis for implementing a scheme interpreter for now: draw EM state with boxes and pointers later on: implement with code 3

4 A shift in viewpoint As we introduce the environment model, we are going to shift our viewpoint on computation Variable: OLD name for value NEW place into which one can store things Procedure: OLD functional description NEW object with inherited context Expressions Now only have meaning with respect to an environment 4

5 Frame: a table of bindings Binding: Example: a pairing of a name and a value x is bound to 15 in frame A y is bound to (1 2) in frame A the value of the variable x in frame A is 15 A x: 15 y: 1 2 5

6 Environment: a sequence of frames Environment consists of frames A and B Environment E2 consists of frame B only A frame may be shared by multiple environments B E2 A z: 10 x: 15 y: this arrow is called the enclosing environment pointer 1 2 6

7 Evaluation in the environment model All evaluation occurs in an environment The current environment changes when the interpreter applies a procedure The top environment is called the global environment () Only the has no enclosing environment To evaluate a combination Evaluate the subexpressions in the current environment Apply the value of the first to the values of the rest 7

8 Name-rule A name X evaluated in environment E gives the value of X in the first frame of E where X is bound z ==> 10 z ==> 10 x ==> 15 In, the binding of x in frame A shadows the binding of x in B B z: 10 x: 3 x ==> 3 A x: 15 y: 1 2 8

9 Define-rule A define special form evaluated in environment E creates or replaces a binding in the first frame of E (define z 20) (define z 25) B z: 10 x: 3 z: 20 z ==> 20 A x: 15 y: z: 25 z ==>

10 Set!-rule A set! of variable X evaluated in environment E changes the binding of X in the first frame of E where X is bound (set! z 20) (set! z 25) B z: 10 x: A x: 15 y:

11 Define versus Set! Using defines Using set!s B z: 10 x: 3 z: 20 B z: x: 3 A x: 15 y: z: 25 A x: 15 y:

12 Your turn: evaluate the following in order (+ z 1) ==> (set! z (+ z 1)) (define z (+ z 1)) (set! y (+ z 1)) B z: 10 x: 3 11 (modify EM) (modify EM) (modify EM) 11 Error: unbound variable: y A x: 15 y: z:

13 Double bubble: how to draw a procedure (lambda (x) (* x x)) #[compound-...] A compound proc that squares its argument Environment pointer Code pointer parameters: x body: (* x x) 15

14 Lambda-rule A lambda special form evaluated in environment E creates a procedure whose environment pointer is E (define square (lambda (x) (* x x))) B A z: 10 x: 3 x: 15 square: Evaluating a lambda actually returns a pointer to the procedure object parameters: x body: (* x x) environment pointer points to frame A because the lambda was evaluated in and A 16

15 To apply a compound procedure P to arguments: 1. Create a new frame A 2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P 3. In A, bind the parameters of P to the argument values 4. Evaluate the body of P with E as the current environment 17

16 Achieving Inner Peace (and A Good Grade), Part II * 1. Create a new frame A 2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P 3. In A, bind the parameters of P to the argument values 4. Evaluate the body of P with E as the current environment *Om Mani Padme Hum 18

17 (square 4) x: 10 square: *: #[prim] square parameters: x body: (* x x) ==> #[proc] A x: 4 (* x x) ==> 16 * ==> #[prim] x ==> 4 19

18 Example: inc-square inc-square: square: p: x b: (* x x) p: y b: (+ 1 (square y)) (define square (lambda (x) (* x x))) (define inc-square (lambda (y) (+ 1 (square y))) 20

19 Example cont'd: (inc-square 4) inc-square: square: y: 4 p: x b: (* x x) p: y b: (+ 1 (square y)) (+ 1 (square y)) + ==> #[prim] (square y) inc-square ==> #[compound-proc...] 21

20 Example cont'd: (square y) inc-square: square: y: 4 E2 x: 4 p: x b: (* x x) p: y b: (+ 1 (square y)) (+ 1 (square y)) + ==> #[prim] (square y) square ==> #[compound] y ==> 4 (* x x) E2 ==> 16 (+ 1 16) ==> 17 * E2 ==> #[prim] x E2 ==> 4 22

21 Lessons from the inc-square example EM doesn't show the complete state of the interpreter missing the stack of pending operations The contains all standard bindings (*, cons, etc) omitted from EM drawings Useful to link environment pointer of each frame to the procedure that created it 23

22 Example: make-counter Counter: something which counts up from a number (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 ; not functional programming (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 (cb) ==> 2 ; ca and cb are independent 24

23 (define ca (make-counter 0)) make-counter: ca: p: n b:(lambda () (set! n (+ n 1)) n) n: 0 environment pointer points to because the lambda was evaluated in p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) 25

24 (ca) ==> 1 make-counter: ca: p: n b:(lambda () (set! n (+ n 1)) n) n: 0 E2 empty p: b:(set! n (+ n 1)) n 1 (set! n (+ n 1)) E2 n E2 ==> 1 26

25 (ca) ==> 2 make-counter: ca: p: n b:(lambda () (set! n (+ n 1)) n) n: E3 empty p: b:(set! n (+ n 1)) n (set! n (+ n 1)) E3 n E3 ==> 2 27

26 (define cb (make-counter 0)) make-counter: ca: cb: p: n b:(lambda () (set! n (+ n 1)) n) n: 2 E3 p: b:(set! n (+ n 1)) n E4 n: 0 p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) E4 28

27 (cb) ==> 1 make-counter: ca: cb: p: n b:(lambda () (set! n (+ n 1)) n) n: 2 E2 p: b:(set! n (+ n 1)) n E4 n: 0 1 p: E5 b:(set! n (+ n 1)) n 29

28 Capturing state in local frames & procedures make-counter: ca: cb: p: n b:(lambda () (set! n (+ n 1)) n) n: 2 E2 p: b:(set! n (+ n 1)) n E4 n: 1 p: b:(set! n (+ n 1)) n 30

29 Lessons from the make-counter example Environment diagrams get complicated very quickly Rules are meant for the computer to follow, not to help humans A lambda inside a procedure body captures the frame that was active when the lambda was evaluated this effect can be used to store local state 31

30 Environments are important in other languages USA Britain New England Canada Unbound variable!! Macintosh USA Milkshake USA Canadian bacon New England Macintosh Britain Frappe Milkshake New New England England Canadian bacon Canada 32

### Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns Previous lecture Basics of Scheme Expressions and associated values (or syntax and

### Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

### Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

### Evaluating Scheme Expressions

Evaluating Scheme Expressions How Scheme evaluates the expressions? (procedure arg 1... arg n ) Find the value of procedure Find the value of arg 1 Find the value of arg n Apply the value of procedure

### Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

### CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

### Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

### regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

1 File: regsim.scm Register machine simulator from section 5.2 of STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS This file can be loaded into Scheme as a whole. Then you can define and simulate machines

### Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Lexical Binding There are two ways a variable can be used in a program: As a declaration As a "reference" or use of the variable Scheme has two kinds of variable "declarations" -- the bindings of a let-expression

### Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process

### Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

### bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

Topic 18 Environment Model of Evaluation Section 3.2 bindings (review) a binding is an association between a name and a Scheme value names: variable names, procedure names formal parameters of procedures

### CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the

### Lecture 4. Part 1. More on "First-Class" Procedures

Lecture 4. Part 1. More on "First-Class" Procedures 4.1 The Lambda Special Form An unnamed "doubling" procedure: (lambda (n) (* 2 n)) Think of this as saying "The procedure that takes an argument n, and,

### UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division CS61A Kurt Meinz Structure and Interpretation of Computer Programs Summer 2002

### 6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

### Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

### April 2 to April 4, 2018

MORE SCHEME COMPUTER SCIENCE MENTORS 61A April 2 to April 4, 2018 1 Scheme 1. What will Scheme output? Draw box-and-pointer diagrams to help determine this. (a) (cons (cons 1 nil) (cons 2 (cons (cons 3

### Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

### Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

6.001 SICP Variations on a Scheme Scheme Evaluator A Grand Tour Techniques for language design: Interpretation: eval/appl Semantics vs. snta Sntactic transformations Building up a language... 3. 1. eval/appl

### CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise If you re not already crazy about Scheme (and I m sure you are), then here s something to get

### CSE413 Midterm. Question Max Points Total 100

CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,

### 6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

### Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Discussion 4. Data Abstraction and Sequences

Discussion 4 Data Abstraction and Sequences Data Abstraction: The idea of data abstraction is to conceal the representation of some data and to instead reveal a standard interface that is more aligned

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1

### An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

### Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### ENVIRONMENT MODEL: FUNCTIONS, DATA 18

ENVIRONMENT MODEL: FUNCTIONS, DATA 18 COMPUTER SCIENCE 61A Jon Kotker and Tom Magrino July 18, 2012 1 Motivation Yesterday, we introduced the environment model of computation as an alternative to the earlier

### Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### CS61A Midterm 2 Review (v1.1)

Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

### Berkeley Scheme s OOP

Page < 1 > Berkeley Scheme s OOP Introduction to Mutation If we want to directly change the value of a variable, we need a new special form, set! (pronounced set BANG! ). (set! )

### STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? A stream is an element and a promise to evaluate the rest of the stream. Streams are a clever idea that allows one to use sequence

### CS 61A Discussion 8: Scheme. March 23, 2017

CS 61A Discussion 8: Scheme March 23, 2017 Announcements Ants is due today. Finish it! Also turn it in! HW 6 is due tomorrow. Finish it! Also turn it in! HW party today from 6:30-8:30p in 247 Cory. Midterm

### Parsing Scheme (+ (* 2 3) 1) * 1

Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes

### Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

### Discussion 12 The MCE (solutions)

Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of

### SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Discussion 11. Streams

Discussion 11 Streams A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the books, so I will not dwell

### University of Massachusetts Lowell

University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

### Programming Project #4: A Logo Interpreter Summer 2005

CS 61A Programming Project #4: A Logo Interpreter Summer 2005 In Chapter 4 we study a Scheme interpreter written in Scheme, the metacircular evaluator. In this project we modify that evaluator to turn

### CSCE 531, Spring 2015 Final Exam Answer Key

CSCE 531, Spring 2015 Final Exam Answer Key 1. (40 points total) Consider the following grammar with start symbol S : S S S asb S T T T a T cs T ɛ (a) (10 points) Find FIRST(S), FIRST(T ), FOLLOW(S), and

### Functional Programming

Functional Programming CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario When the Function is the Thing In O-O programming, you typically know where an action is needed,

### Chapter 1. Fundamentals of Higher Order Programming

Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

### 6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

1 of 10 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 3 solutions 1. Box and pointer. > (let ((x (list 1 2 3))) (set-car! (cdr x) (cddr x)) x) (1 (3) 3) +-------------+ V --------- -- ------ ---------

### SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### DRAWING ENVIRONMENT DIAGRAMS

DRAWING ENVIRONMENT DIAGRAMS COMPUTER SCIENCE 61A September 10, 2012 0.1 Background A frame is a location where variable bindings are stored A binding is a connection between a name and a value. The name

### CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

### Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

### SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### C311 Lab #3 Representation Independence: Representation Independent Interpreters

C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction

### Faced with the choice between changing one s mind and proving that there is no need to do so, almost everyone gets busy on the proof.

Chapter 9 State Faced with the choice between changing one s mind and proving that there is no need to do so, almost everyone gets busy on the proof. John Kenneth Galbraith The subset of Scheme we have

### An Introduction to Scheme

An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification

### Python in 10 (50) minutes

Python in 10 (50) minutes https://www.stavros.io/tutorials/python/ Python for Microcontrollers Getting started with MicroPython Donald Norris, McGrawHill (2017) Python is strongly typed (i.e. types are

### Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

CSE 3302 Test 1 1. Preprocessor macros are associated with: A. C B. Java C. JavaScript D. Pascal 2. (define x (lambda (y z) (+ y z))) is an example of: A. Applying an anonymous function B. Defining a function

### Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

Answers to CS61 A Final of May 23, 1997 We repeat the questions; some have typos corrected with respect to the actual exam handed out. Question 1 (5 points): Let us represent a set S of unique expressions,

### 6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane Boning Further Variations on a Scheme Beyond Scheme

### Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004) There are only 5 ideas in Computer Science, and by the end of this course, you ll know 3 of them --Harry Mairson

### CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

### Principles of Programming Languages

Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

### 6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

### An Explicit Continuation Evaluator for Scheme

Massachusetts Institute of Technology Course Notes 2 6.844, Spring 05: Computability Theory of and with Scheme February 17 Prof. Albert R. Meyer revised March 3, 2005, 1265 minutes An Explicit Continuation

### Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1 Review Functions as first-class objects What can you do with an integer?

### Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

### Lecture 2: SML Basics

15-150 Lecture 2: SML Basics Lecture by Dan Licata January 19, 2012 I d like to start off by talking about someone named Alfred North Whitehead. With someone named Bertrand Russell, Whitehead wrote Principia

### Class Structure. Prerequisites

Class Structure Procedural abstraction and recursion 6.037 - Structure and Interpretation of Computer Programs Mike Phillips, Benjamin Barenblat, Leon Shen, Ben Vandiver, Alex Vandiver, Arthur Migdal Massachusetts

### Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Today's Topics Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the (LL,ON) addressing Today Maintaining the Kinds of parameters and parameter passing (LL,ON) Address

### Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

Lecture 8: Recursing Lists CS15: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Defining Recursive Procedures 1. Be optimistic! Assume you can solve

### CSE 341: Programming Languages

CSE 341: Programming Languages Winter 2005 Lecture 17 varargs and apply, implementing higher-order functions CSE341 Winter 2005, Lecture 17 1 Today: Some easy Scheme odds and ends Implementing higher-order

### SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

### Principles of Programming Languages

Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

### 6.001 Notes: Section 5.1

6.001 Notes: Section 5.1 Slide 5.1.1 In this lecture we are going to continue with the theme of building abstractions. Thus far, we have focused entirely on procedural abstractions: the idea of capturing

### Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

### CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CS61A Notes 02b Fake Plastic Trees Box and Pointer Diagrams QUESTIONS: Evaluate the following, and draw a box-and-pointer diagram for each. (Hint: It may be easier to draw the box-and-pointer diagram first.)

### An Explicit-Continuation Metacircular Evaluator

Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

### CS61A Notes Disc 11: Streams Streaming Along

CS61A Notes Disc 11: Streams Streaming Along syntax in lecture and in the book, so I will not dwell on that. Suffice it to say, streams is one of the most mysterious topics in CS61A, trust than whatever

### Programming Languages and Techniques (CIS120)

Programming Languages and Techniques () Lecture 13 February 12, 2018 Mutable State & Abstract Stack Machine Chapters 14 &15 Homework 4 Announcements due on February 20. Out this morning Midterm results

### Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after) Everyone with a partner that wants a partner? Extra Office Hours on Sunday 3/28 in C50 from 1pm Midterm 3 on 4/14

### Project 2: Scheme Interpreter

Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

### Box-and-arrow Diagrams

Box-and-arrow Diagrams 1. Draw box-and-arrow diagrams for each of the following statements. What needs to be copied, and what can be referenced with a pointer? (define a ((squid octopus) jelly sandwich))

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

### 6.001: Structure and Interpretation of Computer Programs. Themes to be integrated. Our target the art of M. C. Escher

6.001: Structure and Interpretation of Computer Programs Today Building a new language using data and procedure abstractions Themes to be integrated Data abstraction Separate use of data structure from

### Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

### CS 415 Midterm Exam Spring SOLUTION

CS 415 Midterm Exam Spring 2005 - SOLUTION Name Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you

### Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)

### A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

### CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is

### Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters Corky Cartwright January 26, 2018 Denotational Semantics The primary alternative to syntactic semantics is denotational semantics.

### Type Systems, Type Inference, and Polymorphism

6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

### This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

CS 61A Final Exam May 16, 2008 Your name login: cs61a This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions. This booklet contains 18 numbered pages including

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

### CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

Haskell, Part 5 CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4 More Haskell Miscellaneous topics Simple I/O, do blocks, actions Modules data vs type declaration Instances of classtypes

### Fall Semester, Lecture Notes { September 26, Today's lecture will use the Henderson Picture Language as an example of how we can merge

1 MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { September 26,

### Intro. Scheme Basics. scm> 5 5. scm>

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if