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

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

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

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

Discussion 12 The MCE (solutions)

4.2 Variations on a Scheme -- Lazy Evaluation

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

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

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

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

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


Sample Final Exam Questions

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

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

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

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

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

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,

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

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


Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Building a system for symbolic differentiation

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

SCHEME AND CALCULATOR 5b

Lexical vs. Dynamic Scope

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

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

CS 61A Midterm #2 - October 5, 1998

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

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

COMP 105 Homework: Type Systems

Functional Programming. Pure Functional Programming

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

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

6.001: Structure and Interpretation of Computer Programs

CSE341 Spring 2017, Final Examination June 8, 2017

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Building a system for symbolic differentiation

6.001 Notes: Section 8.1

Structure and Interpretation of Computer Programs

CSE413 Midterm. Question Max Points Total 100

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

Structure and Interpretation of Computer Programs

CS61A Midterm 2 Review (v1.1)

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

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

An Explicit-Continuation Metacircular Evaluator

CS 360 Programming Languages Interpreters

Administrivia. Simple data types

YOUR NAME PLEASE: *** SOLUTIONS ***

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

CS3 Midterm 1 Fall 2006

Documentation for LISP in BASIC

6.001 Notes: Section 15.1

CS 415 Midterm Exam Fall 2003

Structure and Interpretation of Computer Programs

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

Project 5 - The Meta-Circular Evaluator

CSE341 Spring 2017, Final Examination June 8, 2017

CS 314 Principles of Programming Languages. Lecture 16

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

CS 275 Name Final Exam Solutions December 16, 2016

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

CS450 - Structure of Higher Level Languages

Structure and Interpretation of Computer Programs

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

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

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

From Syntactic Sugar to the Syntactic Meth Lab:

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

Structure and Interpretation of Computer Programs

Programming Project #4: A Logo Interpreter Summer 2005

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

Project 5 - The Meta-Circular Evaluator

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

CPSC W1: Midterm 1 Sample Solution

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

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

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

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

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

CPSC W2 Midterm #2 Sample Solutions

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

CSc 520 Principles of Programming Languages

CS61A Notes Disc 11: Streams Streaming Along

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

Structure and Interpretation of Computer Programs

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

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

The SUMO Speaker Series for Undergraduates. The Art Gallery Problem

Project 2: Scheme Interpreter

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Programming Languages (CS 550) Simple Query Language*

University of Massachusetts Lowell

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

Transcription:

CS 60A Final May 16, 1992 Your name Discussion section number TA's name This exam is worth 30 points, or 18.75% of your total course grade. The exam contains six questions. This booklet contains eleven numbered pages (both sides of six sheets) 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. Our expectation is that many of you will not complete one or two of these questions. If you nd one question especially dicult, leave it for later start with the ones you nd easier. 1 =5 2 =5 3 =5 4 =5 5 =5 6 =5 1 total =30

Question 1 (5 points): Every integer greater than 1 has a unique prime factorization. That is, there is exactly one way that each integer can be expressed as a product of prime numbers. For example, 12 is 2 23. 13 is just 13. 14 is 2 7. 15 is 3 5. (Notice that the same prime can be used more than once, as in the case of 12.) You are given the streams integers (containing all the positive integers) and primes (containing all the prime numbers). (You need not reproduce the computations in the book that generate these streams.) (a) Write the function factors that takes an integer as its argument and returns a list (not a stream) containing its prime factorization. For example: > (factors 12) (2 2 3) > (factors 13) (13) (b) Construct the stream factor-stream that contains the prime factorizations of all the positive integers. 2

Your name Question 2 (5 points): We're going to create an abstract data type for times of day (hours and minutes). To make things simpler, we'll use 24-hour times, in which 3pm is hour 15. You are going to implement this abstract data type using two dierent internal representations. The constructor for times of day is called time. It takes two arguments: the hours and the minutes. So 4:12am would be (time 4 12). We also want the following three operators for times: (hour t) returns the hour portion of the given time. (minute t) returns the minute portion of the given time. (hour? t) returns #t (true) if the time t is an exact hour time (like 6:00) it returns #f (false) otherwise. (a) Implement time, hour, minute, and hour? using an internal representation in which a time is represented as a list of two numbers. That is, 4:12am should be represented internally as the list (4 12). This question continues on the following page. 3

Question 2 continued: (b) Implement time, hour, minute, and hour? using an internal representation in which a time is represented as an integer, namely, the number of minutes since midnight. That is, 4:12am should be represented internally as the number 252 (4 60 + 12). 4

Your name Question 3 (5 points): King Arthur had a problem. His daughter, Glissanda, was interested in marrying. But she had one requirement for a husband: he must love mathematics. Arthur was confused about how to nd such ahusband. After all, the Knights of the Round Table (the best men in the land) were outdoor types who went around slaying dragons. He couldn't remember any of them ever mentioning mathematics. Glissanda suggested that he administer a mathematical test to the Knights. She explained, \Suppose 24 knights came to a meeting of the Round Table. And suppose the 24 chairs were numbered in order, from 1 to 24. In order to choose my husband, you draw your sword, point to the knight in the rst chair, and say, `You live.' Then point to the knight in chair number 2, say `You die,' and chop o his head. To the third knight you say, `You live,' to the fourth, `you die,' and so on, chopping o the head of every other living knight until just one is left. That's the one I'll marry." \That's it?" asked Arthur, horried. \You expect me to kill all my knights but one? Is this what you call mathematics?" \Oh, father," Glissanda said, \I wouldn't expect you to actually kill anyone. It's just a problem. The knight of my dreams will be able to gure out which chair to choose no matter how many knights show up for the test." [Adapted from Math for Smarty Pants by Marilyn Burns. Illustrations by Martha Weston. Copyright c 1982 by the Yolla Bolly Press.] We want to be able to simulate this process of elimination for dierent numbers of knights. To this end you'll dene a knight object class. A knight is created with one instantiation variable, the chair number in which he is seated. Each knight object must keep track of its (still living) neighbors to the left and to the right. A knight accepts the following messages: This question continues on the following page. 5

Question 3 continued: Set-left-neighbor! takes a knight as argument and sets the recipient knight's left neighbor to the argument knight. Set-right-neighbor! takes a knight as argument and sets the recipient knight's right neighbor to the argument knight. Skip (with no arguments) tells the recipient that he's been skipped. The recipient should send a die message to his left neighbor, unless he is the only knight left alive, in which case the method should return his number as the winning knight. Die (with no arguments) tells the recipient that he's been killed. The recipient should remove himself from the circle (by telling his neighbors to adjust their neighbors) print a message like knight 12 dies and send a skip message to his left neighbor. Example: To run a simulation with three knights, you would do this: (define knight-1 (instantiate knight 1)) (define knight-2 (instantiate knight 2)) (define knight-3 (instantiate knight 3)) (ask knight-1 'set-right-neighbor! knight-2) (ask knight-2 'set-right-neighbor! knight-3) (ask knight-3 'set-right-neighbor! knight-1) (ask knight-1 'set-left-neighbor! knight-3) (ask knight-2 'set-left-neighbor! knight-1) (ask knight-3 'set-left-neighbor! knight-2) (ask knight-1 'skip) 6

Your name Question 4 (5 points): Write the function cxr-function. This function must take as its argument a symbol such as CDDDADDAADAR and returns the function that should have that name!(note, this is not the same as the bonus problem we gave earlier. The bonus problem went from function to name. We want to go from name to function. This is much easier.) You may assume that the argument isasymbol that starts with C, ends with R, and has some combination of As and Ds in between. You must return a function of one argument that returns the result of the corresponding composition of cars and cdrs of that argument. The symbol may be of any length. Examples: > ((cxr-function 'cadr) '(a b c d e)) B > (define func (cxr-function 'cddaddadr)) FUNC > (func '((1 2 3) (x y (red orange yellow green blue) z) (a b c))) (YELLOW GREEN BLUE) 7

Question 5 (5 points): (a) Write a logic program (one or more rules) to implement the substring relation. It takes two lists and it succeeds if one is a substring of the other. For example, the query (substring (to hold your) (i want to hold your hand)) should succeed, but the query (substring (to hold hand) (i want to hold your hand)) should fail. (Hint: One possible solution uses the append rules another possible solution doesn't require append.) (b) We don't expect you to be able to predict exactly what'll happen, but assuming there are no strange bugs, what might you reasonably expect if you tried the query (substring?x (a b c)) "Done" with no matches reported. "Done" with one match reported. "Done" with more than one correct match reported. An innite loop. (c) What might you reasonably expect from the query (substring (a b c)?x) "Done" with no matches reported. "Done" with one match reported. "Done" with more than one correct match reported. An innite loop. 8

Your name Question 6 (5 points): Alyssa P. Hacker has noticed that many 60A students lose points in midterm 3 by leaving out the empty frames that you get from invoking a procedure with no arguments. She asks all her friends how she can teach the students better so they won't make this mistake. Ben Bitdiddle says, \Instead of teaching the students better, let's just modify the metacircular evaluator so that it doesn't create frames that would be empty. Then the students will be right." Modify the metacircular evaluator to implement Ben's suggestion. (a) Is this primarily a change to eval or to apply? (b) What specic procedure(s) will you change? (c) Make the changes on the following pages. (d) Lem E. Tweakit notices that this change is not such a great idea. He thinks the modied metacircular evaluator will interpret certain Scheme programs incorrectly. Under what circumstances would this modication to the evaluator change the meaning of Scheme programs run using the metacircular evaluator? (That is, what kind of program will produce dierent results using the modied version than using the original one?) This question continues on the following page. 9

Question 6 continued: (define (eval exp env) (cond ((self-evaluating? exp) exp) ((quoted? exp) (text-of-quotation exp)) ((variable? exp) (lookup-variable-value exp env)) ((definition? exp) (eval-definition exp env)) ((assignment? exp) (eval-assignment exp env)) ((lambda? exp) (make-procedure exp env)) ((conditional? exp) (eval-cond (clauses exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -- EVAL" exp)))) (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type -- APPLY" procedure)))) (define (list-of-values exps env) (cond ((no-operands? exps) '()) (else (cons (eval (first-operand exps) env) (list-of-values (rest-operands exps) env))))) (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else (eval (first-exp exps) env) (eval-sequence (rest-exps exps) env)))) This question continues on the following page. 10

Your name Question 6 continued: (define (extend-environment variables values base-env) (adjoin-frame (make-frame variables values) base-env)) (define (adjoin-frame frame env) (cons frame env)) (define (make-frame variables values) (cond ((and (null? variables) (null? values)) '()) ((null? variables) (error "Too many values supplied" values)) ((null? values) (error "Too few values supplied" variables)) (else (cons (make-binding (car variables) (car values)) (make-frame (cdr variables) (cdr values)))))) (define (make-binding variable value) (cons variable value)) (define (eval-definition exp env) (define-variable! (definition-variable exp) (eval (definition-value exp) env) env) (definition-variable exp)) (define (make-procedure lambda-exp env) (list 'procedure lambda-exp env)) 11