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

Similar documents
A random five-digit number: a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Discussion 12 The MCE (solutions)

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

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

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name


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

Scheme Basics > (butfirst '(help!)) ()

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

CS61A Midterm 2 Review (v1.1)

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

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

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

Lexical vs. Dynamic Scope

Turtles All The Way Down

An Explicit-Continuation Metacircular Evaluator

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

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

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

CS 275 Name Final Exam Solutions December 16, 2016

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

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

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

Sample Final Exam Questions

CS 61A Midterm #2 - October 5, 1998

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

Functional Programming. Pure Functional Programming

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

CSc 520 Principles of Programming Languages

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

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

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

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

University of Massachusetts Lowell

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

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


SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

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

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

Discussion 4. Data Abstraction and Sequences

An Introduction to Scheme

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

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

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

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))

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

An Explicit Continuation Evaluator for Scheme

SCHEME AND CALCULATOR 5b

Project 2: Scheme Interpreter

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

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

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

Introduction to Scheme

Functional Programming

Evaluating Scheme Expressions

Programming Project #4: A Logo Interpreter Summer 2005

Structure and Interpretation of Computer Programs

CS450 - Structure of Higher Level Languages

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

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

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

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

Documentation for LISP in BASIC

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

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

4.2 Variations on a Scheme -- Lazy Evaluation

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

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

CS 314 Principles of Programming Languages. Lecture 16

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

6.001: Structure and Interpretation of Computer Programs

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CSE413 Midterm. Question Max Points Total 100

YOUR NAME PLEASE: *** SOLUTIONS ***

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

Administrivia. Simple data types

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

From Syntactic Sugar to the Syntactic Meth Lab:

Building a system for symbolic differentiation

CSE341 Spring 2017, Final Examination June 8, 2017

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Repetition Through Recursion

Berkeley Scheme s OOP

Structure and Interpretation of Computer Programs

Fall 2017 December 4, Scheme. Instructions

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

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

Project 5 - The Meta-Circular Evaluator

Fall 2017 December 4, 2017

Comp 311: Sample Midterm Examination

Transcription:

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 the cover page. Put all answers on these pages, please; don t hand in stray pieces of paper. This is an open book exam. When writing procedures, don t put in error checks. Assume that you will be given arguments of the correct type. If you want to use procedures defined in the book or reader as part of your solution to a programming problem, you must cite the page number on which it is defined so we know what you think it does. *** IMPORTANT *** Check here if you are one of the people with whom we arranged to replace a missed/missing exam with other exam scores: *** IMPORTANT *** If you have made grading complaints that have not yet been resolved, put the assignment name(s) here: READ AND SIGN THIS: I certify that my answers to this exam are all my own work, and that I have not discussed the exam questions or answers with anyone prior to taking this exam. If I am taking this exam early, I certify that I shall not discuss the exam questions or answers with anyone until after the scheduled exam time. 1 /4 2 3 /7 4 5 /7 6 /7 7 8 /7 9 /3 10 /3 11 12 /8 13 /8 14 /8 15 /8 total /70 1

Question 1 (4 points): What will the Scheme interpreter print in response to each of the following expressions? Also, draw a box and pointer diagram for the result of each printed expression. If any expression results in an error, just write ERROR ; you don t have to give the precise message. Hint: It ll be a lot easier if you draw the box and pointer diagram first! (let ((foo (list a b c)) (bar (list x y z))) (set-car! (cdr foo) (cadr bar)) (set-car! (cddr foo) (cdr bar)) foo) (let ((foo (list a b c)) (bar (list x y z))) (set-cdr! (cddr bar) (cdr foo)) (set-cdr! (cdr bar) (car foo)) bar) 2

Your name login cs61a Question 2 (3 points): (define (foo x y) (+ x (* y x))) (foo (* 2 2) (+ 8 4)) How many times are + and * called from the invocation of foo above... in applicative order? + invocations * invocations in normal order without memoization? + invocations * invocations in normal order with memoization? + invocations * invocations Question 3 (4 points): Mark whether each of the following generate an iterative or recursive process by circling the appropriate word, and fill in the order of growth: (define (foo x) ; Assume factorial is Θ(n) (define (helper n) (if (= n x) 0 (* (factorial n) (helper (+ n 1))))) (helper 0)) ; ITERATIVE / RECURSIVE Θ( ) (define (mystery sent) (define (help sent result) (if (empty? sent) result (if (member? (first sent) (help (bf sent) result)) (help (bf sent) (se (first sent) result)) (help (bl sent) (se result (last sent)))))) (help sent ())) ; ITERATIVE / RECURSIVE Θ( ) 3

Question 4 (3 points): (define (transform1 f) (lambda (x) (word (f x) (f x)))) (define (transform2 f) (lambda (x) (f (word x x)))) (define (transform3 f) (lambda (x) (f (f x)))) (define f1 (transform1 count)) (define f2 (transform2 count)) (define f3 (transform3 count)) Fill in the blanks with what Scheme returns. (Remember that numbers are words too.) (f1 hello) => (f2 bye) => (f3 abcdefghijklmnopqrstuvwxyz) => Question 5 (4 points): (define (startpoint segment) (car segment)) (define (endpoint segment) (cdr segment)) (define (x-coord point) (car point)) (define (y-coord point) (cdr point)) (define (datum tree) (car tree)) (define (children tree) (cdr tree)) (define (slope segment) (/ (- (cddr segment) (cdar segment)) (- (cadr segment) (caar segment)))) Rewrite slope to fix the data abstraction violations. 4

Your name login cs61a Question 6 (7 points): We are going to add buses to the adventure game. A bus can carry people from one place to another. Here s an example (assuming nasty and hacker are at telegraph): > (define fifty-one (instantiate bus telegraph-ave)) > (ask fifty-one load nasty) > (ask fifty-one load hacker) > (ask fifty-one go south) > (ask fifty-one unload nasty) > (whereis nasty) noahs When the bus moves somewhere, everybody on it should move as well. Implement the bus class. The relevant code from the adventure game is provided on the following pages. HINT: Each of these methods should require very little work from you! (define-class (bus place) (initialize (instance-vars (class-vars (method (load person) (method (unload person) (method (go direction) )) 5

(define-class (thing name) (parent (basic-object)) (instance-vars (possessor no-one)) (method (thing?) #t) (method (type) thing) (method (change-possessor new-possessor) (set! possessor new-possessor)) (method (may-take? who) (cond ((eq? possessor no-one) self) ((> (ask who strength) (ask possessor strength)) self) (else #f))) ) (define-class (place name) (parent (basic-object)) (instance-vars (directions-and-neighbors ()) (things ()) (people ()) (entry-procs ()) (exit-procs ())) (method (place?) #t) (method (type) place) (method (neighbors) (map cdr directions-and-neighbors)) (method (exits) (map car directions-and-neighbors)) (method (look-in direction) (let ((pair (assoc direction directions-and-neighbors))) (if (not pair) () ;; nothing in that direction (cdr pair)))) ;; return the place object (method (appear new-thing) (if (memq new-thing things) (error "Thing already in this place" (list name new-thing))) (set! things (cons new-thing things)) appeared) (method (enter new-person) (if (memq new-person people) (error "Person already in this place" (list name new-person))) (set! people (cons new-person people)) (for-each (lambda (proc) (proc)) entry-procs) (for-each (lambda (pers) (ask pers notice new-person)) (delete new-person people)) appeared) 6

Your name login cs61a (method (gone thing) (if (not (memq thing things)) (error "Disappearing thing not here" (list name thing))) (set! things (delete thing things)) disappeared) (method (exit person) (for-each (lambda (proc) (proc)) exit-procs) (if (not (memq person people)) (error "Disappearing person not here" (list name person))) (set! people (delete person people)) disappeared) ;............... (some methods omitted) ) (define-class (person name place) (parent (basic-object)) (instance-vars (possessions ()) (saying "") (money 100)) (initialize (ask self put strength 100) (ask place enter self)) (method (person?) #t) (method (type) person) (method (lose thing) (set! possessions (delete thing possessions)) (ask thing change-possessor no-one) lost) (method (take-all) (for-each (lambda (thing) (ask self take thing)) (filter (lambda (thing) (eq? (ask thing possessor) no-one)) (ask place things) ) )) (method (go-directly-to new-place) (announce-move name place new-place) (for-each (lambda (p) (ask place gone p) (ask new-place appear p)) possessions) (ask place exit self) (set! place new-place) (ask new-place enter self)) 7

(method (take thing) (cond ((not (thing? thing)) (error "Not a thing" thing)) ((not (memq thing (ask place things))) (error "Thing taken not at this place" (list (ask place name) thing))) ((memq thing possessions) (error "You already have it!")) (else (let ((reply (ask thing may-take self))) (if reply (begin (announce-take name reply) ;; add it to my possessions (set! possessions (cons reply possessions)) ;; go through all the people at the place ;; if they have the object we are taking ;; make them lose it and have a fit (for-each (lambda (pers) (if (and (not (eq? pers self)) ; ignore myself (memq reply (ask pers possessions))) (begin (ask pers lose reply) (have-fit pers)))) (ask place people))) ;; actually change the possessor (ask reply change-possessor self) taken) (announce-too-weak name thing) )))) (method (exits) (ask place exits)) (method (notice person) (ask self talk)) (method (go direction) (let ((new-place (ask place look-in direction))) (cond ((null? new-place) (error "Can t go" direction)) ((not (ask new-place may-enter? self)) (error "can t enter locked place" (ask new-place name))) (else (ask place exit self) (announce-move name place new-place) (for-each (lambda (p) (ask place gone p) (ask new-place appear p)) possessions) (set! place new-place) (ask new-place enter self))))) ) ;............... (some methods omitted) 8

Your name login cs61a Question 7 (3 points): (define wd foo) (define s (make-serializer)) (define (f) (set! wd (word wd d))) (define (g) (set! wd (word wd wd))) (a) List all the possible values of wd after (parallel-execute (s f) (s g)). (b) List all the possible values of wd (starting from its initial value, without part (a) s effect) after (parallel-execute f g). Question 8 (4 points): Consider the following stream procedure: (define (process-stream S) (cons-stream (stream-car S) (stream-filter (lambda (x) (not (equal? x (stream-car S)))) (process-stream (stream-cdr S))))) (a) In one sentence, what function does process-stream compute? (b) We also have these streams: (define ones (cons-stream 1 ones)) (define twos (cons-stream 2 twos)) (define integers (cons-stream 1 (stream-map + ones integers))) Remember that (ss stream 10) displays the first 10 elements of a stream. What does scheme print for each of the following? > (ss (process-stream (interleave ones integers)) 10) > (ss (process-stream (interleave ones twos)) 10) 9

Question 9 (3 points): (define mystery (let ((y 2)) (lambda (z) (let ((x 4)) (lambda () (set! x (+ z y)) (set! y (+ x z)) y))))) (define foo (mystery 3)) ; procedure A (define mystery (lambda (z) (let ((x 4) (y 2)) (lambda () (set! x (+ z y)) (set! y (+ x z)) y)))) (define foo (mystery 3)) ; procedure B (define mystery (lambda (z) (lambda () (let ((x 4) (y 2)) (set! x (+ z y)) (set! y (+ x z)) y)))) (define foo (mystery 3)) ; procedure C On the next page are four environment diagrams. Label three of them A, B, and C, corresponding to the procedures above (write the letter, large, in the global environment box); leave the fourth diagram unlabelled. Question continued on next page. 10

Your name login cs61a Question 9 continued: 11

Question 10 (3 points): For each of the following code sequences, will the analyzing evaluator be faster than the ordinary metacircular evaluator? Assume the evaluator is restarted for each sequence. (a) (define (member thing ls) (cond ((null? ls) #f) ((equal? thing (car ls)) ls) (else (member thing (cdr ls))))) (member 2000 (enumerate-interval 0 1990)) Is (a) faster in the analyzing evaluator? Yes. No. (b) (define (member thing ls) (cond ((null? ls) #f) ((equal? thing (car ls)) ls) (else (member thing (cdr ls))))) (member 0 (enumerate-interval 0 1990)) Is (b) faster in the analyzing evaluator? Yes. No. (c) (define (cube x) (* x x x)) (cube (+ 2 3)) Is (c) faster in the analyzing evaluator? Yes. No. 12

Your name login cs61a Question 11 (4 points): Write a mapreduce program to find how often each letter occurs in a document. (You should use the procedure strip-punctuation, which takes a word and removes all punctuation from it. You don t have to write that procedure.) Question 12 (4 points): The following is run in the nondeterministic evaluator. Fill in the blanks. (define z 7) (define (indecisive x y) (if (amb x (not x)) (begin (set! z 2) (cons z (amb y (+ y 3)))) (cons z (+ y 1)))) (indecisive #t 30) try-again try-again try-again 13

Question 13 (8 points): This question is about the Tree abstract data type, with selectors datum and children. Write a procedure sib-map that works like tree-map, taking a function f and a Tree as arguments and returning a new Tree. The difference is that f is a function of two arguments: the datum and the sibling number, which is 0 for the leftmost child of a node, 1 for the next child, and so on. If my-tree is the following tree: 3 / \ / \ 7 8 / \ / \ 6 2 5 9 (sib-map (lambda (elt posn) (+ (elt (* 10 posn)))) my-tree) would give 3 / \ / \ 7 18 / \ / \ 6 12 25 9 14

Your name login cs61a Question 14 (8 points): Write a query system rule or rules for next-to, a relation among two names and a list, which can determine whether two names are next to each other in the list. For example: (Jerry is next to Evan in (Albert Brian Evan Jerry Ramesh Yaping)) matches the rule, and the query (?who is next to Evan in (Albert Brian Evan Jerry Ramesh Yaping)) yields (Jerry is next to Evan in (Albert Brian Evan Jerry Ramesh Yaping)) (Brian is next to Evan in (Albert Brian Evan Jerry Ramesh Yaping)) Do not use lisp-value! 15

Question 15 (8 points): We want to add optional dynamic scope to the metacircular evaluator. Add a new special form (lambda-dynamic (arg...)...), which creates a dynamic procedure. When a dynamic procedure is invoked, the frame for that procedure should extend the current environment instead of the procedure s creation environment. The relevant metacircular evaluator procedures are listed on the remaining pages of the exam. On this page, write the names of all the procedures that you modify elsewhere. New procedures can go here or on page 18. 16

Your name login cs61a (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (mc-eval (cond->if exp) env)) ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -- EVAL" exp)))) (define (mc-apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type -- APPLY" procedure)))) (define (eval-assignment exp env) (set-variable-value! (assignment-variable exp) (mc-eval (assignment-value exp) env) env) ok) (define (eval-definition exp env) (define-variable! (definition-variable exp) (mc-eval (definition-value exp) env) env) ok) 17

(define (make-procedure parameters body env) (list procedure parameters body env)) (define (compound-procedure? p) (tagged-list? p procedure)) (define (make-frame variables values) (cons variables values)) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame)) (define (add-binding-to-frame! var val frame) (set-car! frame (cons var (car frame))) (set-cdr! frame (cons val (cdr frame)))) (define (extend-environment vars vals base-env) (if (= (length vars) (length vals)) (cons (make-frame vars vals) base-env) (if (< (length vars) (length vals)) (error "Too many arguments supplied" vars vals) (error "Too few arguments supplied" vars vals)))) 18