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

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

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

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

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

CS61A Midterm 2 Review (v1.1)

Functional Programming. Pure Functional Programming

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Project 2: Scheme Interpreter

Documentation for LISP in BASIC

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

Discussion 12 The MCE (solutions)

CS 314 Principles of Programming Languages. Lecture 16

Sample Final Exam Questions

A Brief Introduction to Scheme (II)

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


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

6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

Introduction to Scheme

Comp 311: Sample Midterm Examination

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

CS 314 Principles of Programming Languages

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

An Explicit Continuation Evaluator for Scheme

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Turtles All The Way Down

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

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

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Lexical vs. Dynamic Scope

Evaluating Scheme Expressions

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

From Syntactic Sugar to the Syntactic Meth Lab:

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

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

An Explicit-Continuation Metacircular Evaluator

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

University of Massachusetts Lowell

Building a system for symbolic differentiation

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Project 5 - The Meta-Circular Evaluator

Building a system for symbolic differentiation

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

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Project 5 - The Meta-Circular Evaluator

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

Lists in Lisp and Scheme

ALISP interpreter in Awk

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

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Common LISP-Introduction

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CSE341 Spring 2017, Final Examination June 8, 2017

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

CS 314 Principles of Programming Languages

6.001, Spring Semester, 1998, Final Exam Your Name: 2 Question 1 (12 points): Each of the parts below should be treated as independent. For each part,

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Vectors in Scheme. Data Structures in Scheme

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

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

C311 Lab #3 Representation Independence: Representation Independent Interpreters

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


(scheme-1) has lambda but NOT define

CSE341 Spring 2017, Final Examination June 8, 2017

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

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

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

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

6.945 Adventures in Advanced Symbolic Programming

6.001 Notes: Section 8.1

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

YOUR NAME PLEASE: *** SOLUTIONS ***

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

1.3. Conditional expressions To express case distinctions like

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

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

6.001: Structure and Interpretation of Computer Programs

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

Functional Programming

A Small Interpreted Language

Functional Programming. Pure Functional Languages

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

CSE341 Autumn 2017, Final Examination December 12, 2017

Scheme Quick Reference

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Fall 2017 December 4, 2017

Simplifying Logical Formulas

Administrivia. Simple data types

Functional programming with Common Lisp

INTERPRETERS AND TAIL CALLS 9

Box-and-arrow Diagrams

CS 275 Name Final Exam Solutions December 16, 2016

Scheme Quick Reference

CSc 520 Principles of Programming Languages

CS450 - Structure of Higher Level Languages

Scheme: Strings Scheme: I/O

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Transcription:

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 Fall Semester, 2002 Quiz II Sample solutions Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. Part 1: (18 points) Consider the following procedure: (define (last-call proc) (let ((old *not-used*)) (lambda (arg) (let ((temporary old)) (set! old (proc arg)) temporary)))) For example, we might have: (define weird (last-call (lambda (x) (* x x)))) (weird 5) ;Value: *not-used* (weird 7) ;Value: 25 Suppose we actually evaluate the above sequence of expressions in the order shown: We want you to draw the environment diagram generated by these expressions, using diagram fragments that we provide. Attached to the exam is a tear-off sheet, with some fragments from an environment diagram. In this diagram, we have marked procedure objects as P1, P2, etc., environments as E1, E2, etc. You should EITHER complete this diagram directly on these fragments, OR you should do your own environment diagram on a separate sheet, then copy the labels for the fragments onto your diagram (we actually recommend the latter). In either case, answer the questions about the environment based on the labels used on OUR diagram!! First, for each procedure object, P1 through P4, identify, if possible, the environment pointer of the procedure (i.e. one of GE, E1,..., E8). If the appropriate environment is not shown, write not shown. Question 1. To what does the environment pointer of P1 point?

6.001, Fall Semester, 2002 Quiz II Sample solutions 2 GE Question 2. To what does the environment pointer of P2 point? GE Question 3. To what does the environment pointer of P3 point? E7 Question 4. To what does the environment pointer of P4 point? E6 Second, for each environment frame, indicate which environment is the enclosing environment for that frame. If the appropriate environment is not shown, write not shown. If there is no environment, write none. Question 5. What is the enclosing environment for E1? E2 Question 6. What is the enclosing environment for E2? E6 Question 7. What is the enclosing environment for E3? E6 Question 8. What is the enclosing environment for E4? E3 Question 9. What is the enclosing environment for E5? GE Question 10. What is the enclosing environment for E6? E7 Question 11. What is the enclosing environment for E7? GE For each of the following questions, choose from among these possibilities: one of the procedure objects, P1,..., P5, a number (say what number) a symbol (say what specific symbol) a list of numbers or symbols or procedure objects (which you must draw as box-and-pointer notation), an environment, E1,..., E7, GE,

6.001, Fall Semester, 2002 Quiz II Sample solutions 3 nothing Do this in terms of the values associated with these variables after all the expressions have been evaluated. Question 12. To what is the variable last-call in the global environment bound? P2 Question 13. To what is the variable proc in E7 bound? P1 Question 14. To what is the variable old in E6 bound? 49 Question 15. What variable is bound to the procedure object P4? Give both the name and the environment, if applicable. weird, bound in GE Question 16. What variable is bound to the procedure object P5? Give both the name and the environment, if applicable. nothing Part 2 (18 points): At the end of the quiz you will find the code for our object oriented system. The following object oriented class hierarchy covers different ways objects can hold state. Look over the code carefully before answering the questions. (define (make-a state) (lambda (msg) (case msg ((GETSTATE) (lambda (self) state)) ((ALTER) (lambda (self newstate) (set! state newstate) (ask self GETSTATE))) ((REPORT) (lambda (self) (list (ask self GETSTATE)))) (else (no-method))))) (define (make-b state) (let ((my-a (make-a state))) (lambda (msg) (case msg ((ALTER) (lambda (self newstate) (set! state newstate) (ask self GETSTATE))) ((REPORT) (lambda (self)

6.001, Fall Semester, 2002 Quiz II Sample solutions 4 (cons state (ask my-a REPORT)))) (else (get-method msg my-a)))))) (define (make-c state) (let ((my-a (make-a state)) (my-b (make-b state))) (lambda (msg) (case msg ((GETSTATE) (lambda (self) (ask my-a GETSTATE))) ((ALTER) (lambda (self newstate) (ask my-a ALTER newstate))) ((REPORT) (lambda (self) (delegate my-b self REPORT))) (else (get-method msg my-a)))))) Assume we make the above definitions, and then evaluate (define alex (make-a alpha)) (define bruce (make-b beta)) (define chris (make-c gamma)) What gets returned as value for each of the following expressions? (Assume that they are evaluated in this order.) You may find it helpful to draw an instance diagram or a class diagram to keep track of the state and structure of the objects created in this example. Question 17. (ask alex REPORT) (alpha) Question 18. (ask alex ALTER alef) alef Question 19. (ask alex REPORT) (alef) Question 20. (ask bruce REPORT) (beta beta) Question 21.

6.001, Fall Semester, 2002 Quiz II Sample solutions 5 (ask bruce ALTER betel) beta Question 22. (ask bruce REPORT) (betel beta) Question 23. (ask chris REPORT) (gamma gamma) Question 24. (ask chris ALTER gum) gum Question 25. (ask chris REPORT) (gamma gamma) Part 3 (20 points) We are going to add a new special form to our meta-circular evaluator (a copy of which is attached at the end of the quiz). The form we are going to add is an unless expression, such as (define test (1 2 3)) (unless (null? test) (display (car test)) (set! test (cdr test))) The idea is that an unless expression consists of two parts. The first sub-expression is a test clause, the remaining sub-expressions are the body. The evaluation of an unless is to first evaluate the test. If it is true, then evaluation of the entire unless is terminated, with an unspecified return value. Otherwise, the body of the unless is evaluated and the process repeats. To the meta-circular evaluator, we add the following clause ((unless? exp) (eval-unless exp env)) where

6.001, Fall Semester, 2002 Quiz II Sample solutions 6 (define (unless? exp) (tagged-list? exp unless)) (define (unless-test exp) (cadr exp)) (define (unless-body exp) (cddr exp)) Question 26. Write the procedure eval-unless by completing the following defintion. Be sure to use the appropriate data abstractions. (define (eval-unless exp env) (IF (NOT (M-EVAL (UNLESS-TEST EXP) ENV)) (BEGIN (EVAL-SEQUENCE (UNLESS-BODY EXP) ENV) (EVAL-UNLESS EXP ENV)))) Question 27. Suppose instead that we add the following clause to the evaluator: ((unless? exp) (m-eval (unless->if exp) env)) Complete the syntactic transformation (define (unless->if exp) (LIST IF (LIST NOT (UNLESS-TEST EXP)) (CONS BEGIN (APPEND (UNLESS-BODY EXP) (LIST EXP))))) so that this will correctly evaluate unless expressions. Part 4: (22 points) We want to construct a data abstraction called a queue. A queue consists of an ordered sequence of items, which we will represent using a list. Users of queues see a data abstraction, with a queue consisting of two components, a head and a tail. One can read the element at the head of the queue, but cannot directly access other elements in the queue. Attempting to read an empty queue is an error. One can only add new elements to a queue at the tail of the queue, and one can only remove elements from the head of the queue. For example, here is some code to create queues, and some interactions with a queue. (define (make-queue elt) (let ((temp (list elt))) (cons temp temp))) (define (head q) (car q))

6.001, Fall Semester, 2002 Quiz II Sample solutions 7 (define change-head! set-car!) (define (tail q) (cdr q)) (define change-tail! set-cdr!) (define (read q) (car (head q))) (define (add-queue elt q) (let ((new-part (list elt))) (cond ((null? (tail q)) INSERT-1) (else INSERT-2 INSERT-3))) q) (define (delete-queue q) (cond ((eq? (head q) (tail q)) (change-head! q #f) (change-tail! q #f)) (else INSERT-4)) q) ;; HERE IS AN EXAMPLE QUEUE (define my-queue (make-queue 1)) ;Value: ((1) 1) (read my-queue) ;Value: 1 Question 28: Draw a box and pointer diagram for the value of my-queue. my-queue ------> - --- - \ / \ / / / - ----- \ / 1

6.001, Fall Semester, 2002 Quiz II Sample solutions 8 Now, consider the following actions on a queue: (add-queue 2 my-queue) ;Value: ((1 2) 2) (read my-q) ;Value: 1 ;; NOTE: the same value is still at the head (add-queue 3 my-queue) ;Value: ((1 2 3) 3) (delete-queue my-queue) ;Value: ((2 3) 3) (read my-q) ;Value: 2 ;; NOTE: the value under the head has now changed (delete-queue my-queue) ;Value: ((3) 3) (delete-queue my-queue) ;Value: (#f) ;;NOTE: we are now left with an empty queue Question 29: Provide code for INSERT-1, which should create a queue with one element. Question 30: Provide code for INSERT-2, so that the internal representation of the elements of the queue is modified correctly. Question 31: Provide code for INSERT-3, so that the external representation of the queue is modified correctly. (define (add-queue elt q) (let ((new-part (list elt))) (cond ((null? (tail q)) (CHANGE-HEAD! Q NEW-PART) (CHANGE-TAIL! Q NEW-PART)) (else (SET-CDR! (TAIL Q) NEW-PART) (CHANGE-TAIL! Q (CDR (TAIL Q)))))) q) Question 32: Provide code for INSERT-4. (define (delete-queue q)

6.001, Fall Semester, 2002 Quiz II Sample solutions 9 (cond ((eq? (head q) (tail q)) (change-head! q #f) (change-tail! q #f)) (else (CHANGE-HEAD! Q (CDR (HEAD Q))))) q) Now suppose we decide to change the underlying representation of a queue, to use a list rather than a pair to represent the head and tail of the queue. (define (make-queue elt) (let ((temp (list elt))) (list temp temp))) ;; NOTE THE CHANGE HERE Question 33: What is the minimal set of definitions that must change for the system to still operate correctly? Provide the correct new definitions. (DEFINE (TAIL Q) (CADR Q)) (DEFINE (CHANGE-TAIL! Q NEW) (SET-CAR! (CDR Q) NEW)) Part 5 (22 points): Attached at the end of the quiz is a copy of the meta-circular evaluator. As it presently stands, this version of the evaluator cannot deal with procedures that use the dot notation for their arguments, such as: (define foo (lambda (x. y) (if (> x 0) (length y)))) (foo 1 2 3 4) ;Value: 3 (foo 1) ;Value: 0 (foo 2 5) ;Value: 1 Remember that the idea behind the dot notation was to allow a procedure to be called with variable numbers of arguments. In this example, foo must be called with at least one argument, and the formal parameter x would be bound to that value. However, foo could also be called with 2 or more arguments, and the formal parameter y would then be bound to a list of the values of all the subsequent arguments. Thus in

6.001, Fall Semester, 2002 Quiz II Sample solutions 10 (foo 1 2 3 4) the formal parameter x would be bound to the value 1 and the formal parameter y would be bound to the list (2 3 4). When we evaluate a lambda expression, it will create a representation for a procedure (see make-procedure in the evaluator code). In particular, when the reader converts the input expression such as (lambda (x y)...) into an internal representation for the evaluator, it provides list structure like this: --> - ------> - ------> _ _ _ _ \ / \ / lambda - ------> / _ _ _ _ /_ \ / \ / X Y whereas if the reader is given input such as (lambda (x. structure like this: y)...), this is converted into list --> - ------> - ------> _ _ _ _ \ / \ / lambda - ------> Y _ _ \ / X Thus when we evaluate a lambda expression whose parameter list is of the ordinary sort (e.g., (x y) or (x y z a)), the internal representation of this parameter list is an ordinary list (i.e., a sequence of cons cells whose cdr-pointers end in nil). However, when the parameter list includes a dot (such as (x. y) or (x y z. a)), the representation for this parameter list will not be an ordinary list. It will differ in one small but important detail: the cdr of the last cons cell in the sequence will point to the last parameter (rather than to nil). To change our evaluator to handle dot notation,, we just need to modify extend-environment:

6.001, Fall Semester, 2002 Quiz II Sample solutions 11 (define (extend-environment vars vals base-env) (let ((processed-lists (find-dot vars vals))) (cons (make-frame (car processed-lists) (cadr processed-lists)) base-env))) The find-dot procedure walks down the sequence of variable names and associated values, checking to see whether vars is an ordinary list (i.e., no dot ) or whether it is a sequence of cons cells that terminates by pointing at a variable rather than at nil (i.e., it had a dot). When we find that the cdr of the last cons cell points to a variable name rather than to nil, we want to (a) add that variable to the end of a list of variables, and (b) add the remaining values as a list, which should be added to the end of the list of all values. (Notice that we still add the variable name to a list of variables, but handle the remaining values differently.) Finally, find-dot will return a list of two lists one for the variables, and one for the values. Here is a template for this: (define (find-dot vars vals) (define (help vars-seen vars-todo vals-seen vals-todo) (cond ((null? vars-to-do) ; no more variables to examine (if (null? vals-to-do) ; if no more values either INSERT-5 ; then return collection of vars and vals (error wrong number args vars vals))) ; else report error ((null? (cdr vars-to-do)) ; we re at the last variable and ; there was no dot (if (null? vals-to-do) ; if no more values... (error wrong number args vars vals) ;...report error (if (not (null? (cdr vals-to-do))) ; else if >1 more value (error wrong number args vars vals) ; that s an error too INSERT-6))) ; otherwise, do this ((not (pair? (cdr vars-to-do))) ; sequence of vars doesn t end in nil (if (null? vals-to-do) ; if no values left... (error wrong number args vars vals) ;... report error INSERT-7)) ;... else do this (else ; otherwise collect next var and associated val, and continue (help (cons (car vars-to-do) vars-seen) (cdr vars-to-do) (cons (car vals-to-do) vals-seen) (cdr vals-to-do))))) (help () vars () vals)) Question 34: What code should be used for INSERT-5? (LIST (REVERSE VARS-SEEN) (REVERSE VALS-SEEN))

6.001, Fall Semester, 2002 Quiz II Sample solutions 12 Question 35: What code should be used for INSERT-6? (LIST (REVERSE (CONS (CAR VARS-TO-DO) VARS-SEEN)) (REVERSE (CONS (CAR VALS-TO-DO) VALS-SEEN))) Question 36: What code should be used for INSERT-7? (LIST (REVERSE (CONS (CDR VARS-TO-DO) (CONS (CAR VARS-TO-DO) VARS-SEEN))) (REVERSE (CONS (CDR VALS-TO-DO) (CONS (CAR VALS-TO-DO) VALS-SEEN)))