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

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

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


Discussion 12 The MCE (solutions)

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSE341 Spring 2017, Final Examination June 8, 2017

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

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CSE341 Spring 2017, Final Examination June 8, 2017

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

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


CS 314 Principles of Programming Languages. Lecture 16

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

Lexical vs. Dynamic Scope

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

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

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

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

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

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

CS61A Midterm 2 Review (v1.1)

CS 275 Name Final Exam Solutions December 16, 2016

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

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

Project 2: Scheme Interpreter

Comp 311: Sample Midterm Examination

Structure and Interpretation of Computer Programs

CSE341 Autumn 2017, Final Examination December 12, 2017

An Explicit Continuation Evaluator for Scheme

C311 Lab #3 Representation Independence: Representation Independent Interpreters

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

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

CS 314 Principles of Programming Languages

UMBC CMSC 331 Final Exam

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

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

Structure and Interpretation of Computer Programs

Scheme: Strings Scheme: I/O

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

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

Documentation for LISP in BASIC

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

Discussion 4. Data Abstraction and Sequences

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

A Brief Introduction to Scheme (II)

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

CSE341, Spring 2013, Final Examination June 13, 2013

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

CSE413 Midterm. Question Max Points Total 100

Structure and Interpretation of Computer Programs

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

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

COMP 105 Homework: Type Systems

Sample Final Exam Questions

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

6.001 Notes: Section 15.1

CS 314 Principles of Programming Languages

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

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

Typed Racket: Racket with Static Types

A Small Interpreted Language

Building a system for symbolic differentiation

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

SCHEME AND CALCULATOR 5b

Project 5 - The Meta-Circular Evaluator

Structure and Interpretation of Computer Programs

MIDTERM EXAMINATION - CS130 - Spring 2005

Structure and Interpretation of Computer Programs

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

CS3 Midterm 2 Standards and Solutions

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

University of Massachusetts Lowell

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

4.2 Variations on a Scheme -- Lazy Evaluation

Introduction to Scheme

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

April 2 to April 4, 2018

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

User-defined Functions. Conditional Expressions in Scheme

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

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

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

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

From Syntactic Sugar to the Syntactic Meth Lab:

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Environments

Functional Programming. Pure Functional Languages

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

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

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

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

An Explicit-Continuation Metacircular Evaluator

Turtles All The Way Down

Berkeley Scheme s OOP

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

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

Transcription:

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, atoms or pairs, as a Lisp list of those expressions. As you know, H is a subset of S if each element e of H is also an element of S. The powerset of S is the set of all subsets of S. The powerset of S includes the empty set and S. Here is a recursive way of thinking about powersets as lists: First, the powerset of () is (()). That is, it has one element, namely (). Next, assume that S has at least one element e, and let K be the rest of S. Let P be the powerset of K. The powerset of S is the union of P and a set formed by sticking e in front of each element of P. Here are two examples: The powerset of (1 2 3) is (() (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3)) The powerset of (1 (2 3)) is (() (1) ((2 3)) (1 (2 3))) What is the powerset of '(1 (2 3) 4)? Answer: 1 point. no partial credit ( () (1) ((2 3)) (1 (2 3)) (4) (1 4) ((2 3) 4) (1 (2 3) 4)) all 8 elements. What is the length of the list representing the powerset of '(1 2 3 4 5)? Answer: 1 point. no partial credit. 32. (2^5 = 32) Write a procedure powerset which takes as its only argument a set S and returns the powerset of S. Answer: 3 points total (define (powerset set) (if (null? set) (list '()) ; 1 point for the base case (append (powerset (cdr set)) ; 1 point for the append (map (lambda (s) (cons (car set) s)) ;1 point for the map/lambda (powerset (cdr set)))))) Some people copied the "subsets " program. That was ok except if you called it subsets instead of powerset. Question 2 (7 points): Draw the environment diagram resulting from the following inputs to the Scheme system. What is the value of the global variable x at the end? (define x 10) (define (foo x) (define (bar y) (set! x y) x) bar) ((foo 50) 60) Answer: global env has x:10, foo pointing to function def. 1pt Page 1

sub environment has x:50, then 60 and bar in it. 1 pt sub sub environment has y:50 in it 1 pt correct arrows 2 pt x is 10 at the end 2 pt Question 3 (5 points): Consider the following piece of code: (define foo (lambda (x) (- y x))) (define bar foo) (define y 7) (parallel-execute (lambda () (set! foo (let ((z y)) (lambda (x) (+ (bar x) z))))) (lambda () (set! y (foo 3))) ) What are the possible values of y? ANSWER: 4, 11 (2 pts.) 1 off if you had wrong answers too. The easy part is if branch 2 runs first. y is set to (- 7 3), or 4 Next foo is redefined as though we did (define foo (let ((z y)) (lambda (x) (+ (bar x) z)))) which is actually the same as (define (foo x)(+ (bar x) 4)) where we have just put the value of z, namely the current value of y, namely 4 in there. So foo is (define (foo x)(+ (- y x) 4)) (*) in which case (foo 3) will be (+ (- 4 3) 4) or 5 Another possibility is that branch 1 is executed first, redefining foo as though it were (define (foo x)(+ (bar x)) 7) where we have just put the value of z, namely the current value of y, namely 7 in there. So foo is really the same as (define (foo x)(+ (- y x) 7)) (**) Then running branch2 sets y to (foo 3) which is (+ (- 7 3) 7) or 11. A third possibility is that one starts to run branch 1 and after binding z to y, namely 7, branch 2 is run changing the global y to 4. Then foo is defined as the equivalent of (define (foo x)(+(- y x) 7) with global y being 4. (***) What are the possible results of computing (foo 3) after all the values have settled down? Ans; with foo defined as in (*) and y set to 4, (foo 3) is 5 with foo defined as in (**) and y set to 11 (foo 3) is 15 with foo defined as in (***) and y set to 4 (foo 3) is 8 grading: this was tough. We gave you 3 points if you got at least 2 of these, and no wrong answers. If you got some wrong Page 2

ones we took off 1 Question 4 (8 points): In this age of health-consciousness, the adventure game project needs some revision. We are going to expand the food class and also allow a separate class for fat-free foods. For reference, here is the food class from the online solutions: (define-class (food name calories) (parent (thing name)) (method (edible?) #t)) Your job is to modify this class so that one can type: (instantiate food 'bagel 240 1.333) to create a food object instantiated with the name bagel, a calorie value of 240, and a fat content of 1.333 grams. Additionally, the user should be able to type: (instantiate fatfreefood 'carrot 50) to create a food object (presumably a standard serving of carrot) with the name carrot and a calorie value of 50 (the fat content of 0 will be understood). If obj is a food or fatfreefood object then it should respond as follows: (ask obj 'edible?) should return #t. (ask obj 'name) should return the name of the object. (ask obj 'fat-in-grams) should return obj's fat in grams. (ask obj 'pct-calories-from-fat) should return obj's percent of calories from fat. Each gram of fat contains 9 calories. Therefore our bagel has about 12 calories from fat, and is 12/240 or 5 percent calories from fat. (ask obj 'calories) should return obj's calorie value. (ask obj 'what-are-you) should display a message "I am food." for regular food objects; for fatfreefood objects, it should display whatever message is displayed for regular food objects, plus this message: "I am fat-free." Keep in mind that if we later change the food-description method for regular food objects, the result of asking a fatfreefood what-are-you must change as well. Correct solutions should demonstrate understanding of OOP and inheritance. If your solution is significantly longer than needed it will not get full credit. By the way, a recommended diet has less than 30 percent of calories from fat. Most Americans have an unhealthy 40 percent or more ratio. ANSWER: 5, 3 points for the classes. (define-class (food name calories fat-in-grams) ; 1 Page 3

(parent (thing name)) ; 1 (method (edible?) #t) ; 1 (method (pct-calories-from-fat) ; 1 (* 100 (/ (* fat-in-grams 9) calories))) ;ok if not * 100.0 (method (what-are-you) ; 1 (display "I am food."))) (define-class (fatfreefood name calories) (parent (food name calories 0)) ;1 (method (what-are-you) (usual 'what-are-you) ;1 (display "I am fat-free"))) ;1 Common PITFALLS (1) People defining a redundant calories method. lose a point (2) People defining a redundant name method. ok, since we had a typo of "name?" for name. (3) People failing to use "usual". lose a point (4) People defining unnecessary methods or class-vars, lose a point to handle the fat-content of fatfreefood objects. (5) People not making fatfreefood inherit from food. (6) People returning strings instead of displaying them. lose a pt. Not using inheritance: lose 2 points at least. Question 5 (5 points): This question concerns the Logo interpreter in Project 4. At some points the interpreter detects erroneous constructions and returns to the top-level loop of the Scheme interpreter. We ask you to make a change so that it returns to the top-level of the Logo interpreter. The logo interpreter behaves as follows: > (initialize-logo)? to foo :x -> print 5 -> :x -> print 6 -> end? foo 10 5 ERROR: You don't say what to do with 10 > Note the `` >'' prompt which means you are returning to the Scheme top level. Now modify the above definition of eval-sequence so that the logo interpreter would behave the same as above except... same as above..? foo 10 5 You don't say what to do with 10? In particular, note the ``?'' prompt which means you are returning to the Logo top level. Show your changes by modifying eval-sequence or adding code below. Page 4

(define (eval-sequence exps env) (if (null? exps) '=no-value= (let ((value (eval-line (make-line-obj (car exps)) env))) (cond ((eq? value '=stop=) '=no-value=) ((and (pair? value) (eq? (car value) '=output=)) (cdr value)) ((not (eq? value '=no-value=)) (error "You don't say what to do with" value)) (else (eval-sequence (cdr exps) env)))))) ANSWER: 5 pts total: There were lots of ways of doing this. All involved with something. replacing ;1 pt (where to change) (error "You don't say what to do with" value) Some people figured out that all you had to do was put in value and the error would propagate to the top level, giving the right behavior. This is kind of fragile. It works for this example but is not a good general solution. We expected something like (display "You don't say what to do with ") ;1 pt (logo-show value) ; use the abstraction! ;2 pt (driver-loop) ;1 pt and took points off if you just display'ed value... logo-show or logo-print is better.. initializing logo is NOT right. you need to re-enter the driver-loop. Another solution we allowed used logo-print to give a message and then return =no-value= In fact we gave full or nearly full credit for most solutions that had glitches but showed a basic understanding. Question 6 (10 points): (6.a) Which (if any) of the following expressions provided to the meta-circular evaluator gives the message ``ERROR: Unknown expression type''? (A) #t (B) '(a ()) (C) 0.67 (D) (define x ()) (E) (set! y 'a) (F) true (G) none of the above ANSWER: (d) ;2 pts. () is unknown type ``true'' is actually known. if you say d and f you got credit. (6.b) Which (if any) of the same expressions as the previous question given Page 5

to the meta-circular evaluator gives the message ``ERROR: Unbound variable''? ANSWER: E. y is unbound 2 pts (6.c) How does the meta-circular evaluator know what to do when it encounters the symbol cons in (cons 1 2)? (Circle all that are correct). (A) cons is defined as a lambda (B) cons is defined in the mceval initial environment (C) cons is a scheme primitive (D) cons is a special form (E) none of the above ANSWER: C 2 pts (6.d) In the Amb evaluator, what are the possible expressions that may be produced from (amb 1 (amb 'a (cons 1 2 3) 'd) 4) Note that cons takes only two arguments and therefore its use above causes an error. ANSWER: 1, a 2 pts. (6.e) Stripping away layers of abstraction, we could rewrite a compatible version of the meta-circular evaluator as (define (mc-eval exp env) (cond... ((pair? exp) (mc-apply (mc-eval (car exp) env) (map <B> (cdr exp))))...)) What is the simplest expression you can write for <B>? (hint: under 30 chars.) ANSWER: (lambda(r)(mc-eval r env)) ; 2 pts. 1 pt if partly right Question 7 (3 points): If you load the file defining the meta-circular evaluator and type (mce) twice, you get this result. Explain the ERROR message in one sentence. (mce) ;;; M-Eval input: (mce) ERROR: Unbound variable mce > Answer: 3 pts. partial credit possible. mce is not defined as a function known to the meta-circular evaluation since it is not a scheme primitive or a special form. ( mce is defined in SCHEME by loading in mceval.scm) Question 8 (5 points): Define, using our query system, a palindrome detecting program. A palindrome is a list whose symbols are arranged to read the same forward as backward. Examples are (10 20 20 10) and (hello world hello). We do not mean that individual letter order should be reversed like the English palindrome ``A man a plan a canal panama'' just the symbols taken as units. You may refer to any rules or standard definitions from Page 6

the book or your notes. Hint: This is not a hard problem. ANSWER: (assert! (rule (palin?x) (reverse?x?x))) ; 5 pts 4 points if basically right idea but bad implementation, most based on based on stripping off first and last letters. It is certainly possible to do this correctly, by asserting that that () is a palindrome (?x) is a palindrome, and that?z is a palindrome if?z is the result of appending (?r)?y (?r) and?y is a palindrome. 2 or 3 points if some idea usually, 0 points if you wrote a lisp program or used lispval Question 9 (3 points): The corresponding logo-apply and mc-apply functions are very similar. They differ in their arguments, however. (define (mc-apply procedure arguments)...) (define (logo-apply procedure arguments env)...) Explain, in one sentence, why these differ. logo-apply implements dynamic scope but mc-apply implements static scope. ( ;3 points) You might add that in dynamic scope the environment is passed along with the procedure and its arguments; in static scope, the environment is part of the procedure. Question 10 (6 points): In the homework on the MCEVAL, we offered a solution using the derived-expression technique for expanding an or into one or more if expressions. This is done by adding a clause in the big EVAL cond ((or? exp) (eval (or->if (cdr exp)) env)) and then defining (define (or->if exps) (if (null? exps) #f (make-if (car exps) (car exps) (or->if (cdr exps))))) This is very neat but is unfortunately slightly incorrect. This would be illustrated by evaluating the example (or #f (begin (print 'hi) #t)). What happens and why? ANSWER Assuming print has the side effect of printing, then HI is printed twice, not once. 2 pts (b) Redefine or->if so that it works. Page 7

ANSWER something like: (define (or->if exps) (if (null? exps) #f ;; must evaluate (car exps) only one time (list (list 'lambda '(x) (make-if x x (or->if (cdr exps)))) (car exps))) ) ;4 pts if right or nearly right ;3 pts if obviously not right but close, e.g. e.g. if you used (make-if (car exps) #t (or->if (cdr exps))) which has wrong semantics, but close. ;2 pts some idea ;0 pts no idea Question 11 (3 points): (define (union seq1 seq2) (cond ((null? seq1) seq2) ((not(member (car seq1) seq2)) (union (cdr seq1)(cons (car seq1) seq2))) (else (union (cdr seq1) seq2)))) The worst-case running time of this procedure, as a function of $n$, the length of the longer of seq1 and seq2 is (A) $Theta(1)$ (B) $Theta(log n)$ (C) $Theta(n)$ (D) $Theta(n^2)$ ANSWER D: ; in spite of typos in the actual test, the answer was the same. The worst case would be when the sequences are of equal length n, in which case (member e seq2) is theta(n) where n and this is done n times. Hence theta(n^2). 3 pts. no partial credit Page 8