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

Similar documents
CSE341, Spring 2013, Final Examination June 13, 2013

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

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Spring 2016, Final Examination June 6, 2016

CSE413 Midterm. Question Max Points Total 100

CSE341 Spring 2017, Final Examination June 8, 2017

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Comp 311: Sample Midterm Examination

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

CSE341 Spring 2017, Final Examination June 8, 2017

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

Functional Programming. Pure Functional Programming

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

Box-and-arrow Diagrams

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?

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

April 2 to April 4, 2018

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

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

Project 2: Scheme Interpreter

Warm-up and Memoization

CS61A Midterm 2 Review (v1.1)

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.

CSc 520 Principles of Programming Languages

Sample Final Exam Questions

CS 275 Name Final Exam Solutions December 16, 2016

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

Structure and Interpretation of Computer Programs

Functional Languages. Hwansoo Han

Documentation for LISP in BASIC

Functional Programming. Pure Functional Languages

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

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

Qualifying Exam in Programming Languages and Compilers

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Turtles All The Way Down

Functional Programming. Pure Functional Languages

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 415 Midterm Exam Fall 2003

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

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

CS 314 Principles of Programming Languages. Lecture 16

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

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

Lisp Basic Example Test Questions

CS 415 Midterm Exam Spring SOLUTION

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Lexical vs. Dynamic Scope

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

An Explicit-Continuation Metacircular Evaluator

An Introduction to Scheme

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

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

SCHEME AND CALCULATOR 5b

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

LECTURE 16. Functional Programming

Functions, Conditionals & Predicates

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

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Variables and Bindings

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

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

Functional Programming

Begin at the beginning

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

Functional Programming - 2. Higher Order Functions

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

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

Midterm Examination (Sample Solutions), Cmput 325

G Programming Languages - Fall 2012

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

An introduction to Scheme

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

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

CSE 341 : Programming Languages Midterm, Spring 2015

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

Organization of Programming Languages CS3200/5200N. Lecture 11

low and not larger than high. 18 points

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

CSC 533: Programming Languages. Spring 2015

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

Optimizing Closures in O(0) time

YOUR NAME PLEASE: *** SOLUTIONS ***

COP4020 Programming Assignment 1 - Spring 2011

Overview. CS301 Session 3. Problem set 1. Thinking recursively

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

NOTE: Answer ANY FOUR of the following 6 sections:

Recap: Functions as first-class values

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

A Brief Introduction to Scheme (II)

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Vectors in Scheme. Data Structures in Scheme

Transcription:

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 y 2) (let* ((x 5) (z (* x y))) (+ x z)) 15 (b) (define x 1) (define y 2) (define z (lambda (x) (let ((x (+ x 3))) (+ x y)))) (z 4) 9 (c) (define x 1) (define y 2) (map (lambda (x) (+ x y)) '(3 6-9)) (5 8-7) CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Question 2. (11 points) Suppose we enter the following code at the top level of a Scheme interpreter. (define x 10) (define y 11) (define z 12) (define f (lambda (y) (let ((x 2)) (lambda (z) (+ x y z))))) (define mystery (f 5)) (a) (9 points) Give a precise (but brief) description of the value that is bound to mystery by this sequence of definitions. In other words, what value is returned when function f is applied to the value 5? If the value returned is a function closure, be sure you describe the function as well as the environment bindings (which names are bound to what) in the closure. The result of (f 5) is a function closure, where the code is (lambda (z) (+ x y z)) and the environment includes the bindings (x 2) and (y 5). The full global environment also contains bindings for z and hidden bindings for x and y, but since these variables are not free in the closure they cannot be referenced in the function code, so an implementation may choose not to store these additional bindings in the closure as an optimization. (It s also fine if you said that the code is (+ x y z) with parameter y, since we originally diagramed things that way in class.) (b) (2 points) What value results if we evaluate (mystery 6) after executing the definitions given at the beginning of the problem? 13 CSE 413 Midterm, May 6, 2011 Sample Solution Page 2 of 8

Question 3. (16 points) Suppose we have the following definitions in a Scheme program (define x '(a b c)) (define y (cons 'p 'q)) (define z (cons y x)) (define w (cons 'r x)) (a) Draw a diagram showing the result of evaluating these definitions together in the given order in a newly reset Scheme environment. The first list is drawn for you. x a b c y z p q w r Note that the original list and the (p. q) cons cell are not copied when new cons cells are created that reference them. (b) What are the values of z and w if these are printed by Scheme? z ((p. q) a b c) w (r a b c) CSE 413 Midterm, May 6, 2011 Sample Solution Page 3 of 8

Question 4. (18 points) The MUPL interpreter used association lists to represent environments. An association list is a list of cons pairs where the car of each pair is a name or key of some kind and the cdr is the associated value. This is a very common data structure in Scheme programming, used for many applications besides MUPL environments. For example, here is an association list that uses symbols instead of strings as the key in each pair of the list. ((x. 17) (y. foo) (x. 42) (bar. "secret")) For this problem, write a function (remove key alst) that takes a value key and an association list alst as arguments. Function remove should return a copy of the original list except that all of the pairs in the original list whose car is equal? to key do not appear in the copy. For instance, if pairs is the example list given above, the result of (remove 'x pairs) should be ((y. foo) (bar. "secret")) If key does not appear as the car of any pair in the list, the result should be a copy of the original list. When copying all or part of the list you should not create new copies of the cons pairs themselves (i.e., don t duplicate (y. foo) and similar things). Just copy the list that references them. You should assume that the association list alst has the proper format and you don t need to test for that. Your function should process the list directly and not use any library functions to search or modify the list. It does not need to be tail recursive. It should not define or use any external helper functions. (define (remove key alst) (cond ((null? alst) '()) ((equal? (caar alst) key) (remove key (cdr alst))) (else (cons (car alst) (remove key (cdr alst)))))) CSE 413 Midterm, May 6, 2011 Sample Solution Page 4 of 8

Question 5. (18 points) Write a tail-recursive Scheme function (sum lst) to compute the sum of the integer values in a list. For example, (sum '(4 2 7)) should evaluate to 13. You may assume that the elements of the list are all integers and you do not need to handle the case if they are anything else (including nested lists of integers). For full credit, Your implementation must be properly tail-recursive, and You may not define any additional functions or variables at top-level (i.e., define). You are, of course, free to create any additional functions or variables you wish nested inside the scope of sum, and You should calculate the result directly with appropriate arithmetic operations; you may not use any Scheme library functions like fold or anything similar. (define (sum lst) (letrec ((aux (lambda (acc lst) (if (null? lst) acc (aux (+ acc (car lst)) (cdr lst)))))) (aux 0 lst))) It is also possible to use (define ) to declare the auxiliary function in the inner scope. If this was done correctly it received full credit. CSE 413 Midterm, May 6, 2011 Sample Solution Page 5 of 8

Question 6. (20 points) Our head of sales has reported that we can sell 100,000 units of our MUPL interpreter to a big customer, but only if it includes one more new language feature. We need to add a max expression to the interpreter that computes the maximum of two integer values. (DON T PANIC!!! The answer is considerably shorter than the question!) Here is the specification for the new MUPL max expression: If e 1 and e 2 are MUPL expressions, then (make-max e 1 e 2 ) is a MUPL expression. The value of a max expression is the larger of the two MUPL integer expressions e 1 or e 2. If e 1 and e 2 have the same value, that value is returned. If either expression is not a MUPL integer (i.e., something not created by make-int), then execution should be terminated with a suitable error message. On the next page, write the code needed to add this new expression to the MUPL interpreter evalprog function. Your implementation should evaluate each of the expressions e 1 and e 2 only once (i.e., it should not evaluate the larger expression a second time to compute the final value). You should assume that the following structure has been added to MUPL to represent these conditional expressions: (define-struct max (e1 e2)) ;; = max of expressions e1, e2 For reference, here are the other structures defined in the original MUPL code (most of which you probably won t need): (define-struct var (string)) ;; a variable, e.g., (make-var "foo") (define-struct int (num)) ;; a number, e.g., (make-int 17) (define-struct add (e1 e2)) ;; add two expressions (define-struct ifgreater (e1 e2 e3 e4)) ;; if e1 > e2 then e3 else e4 (define-struct fun (nameopt formal body)) ;; a recursive(?) 1-argument function (define-struct app (funexp actual)) ;; function application (define-struct mlet (var e body)) ;; (let var = e in body) (define-struct apair (e1 e2)) ;; make a new pair (define-struct fst (e)) ;; get first part of a pair (define-struct snd (e)) ;; get second part of a pair (define-struct aunit ()) ;; unit (define-struct isaunit (e)) ;; evaluate to 1 if e is unit else 0 (define-struct closure (env fun)) Reminder: the Scheme function (error "message") can be used to terminate evaluation with the given message. Write your code on the next page. (You can tear this page out of the exam for reference if that is convenient.) CSE 413 Midterm, May 6, 2011 Sample Solution Page 6 of 8

Question 6. (cont.) Write your code to implement the new MUPL max expression below. (define-struct max (e1 e2)) ;; new max expression (define (eval-prog p) (letrec ((f (lambda (env p) (cond (... ((max? p) (let ([v1 (f env (max-e1 p))] [v2 (f env (max-e2 p))]) (if (and (int? v1) (int? v2)) (if (> (int-num v1) (int-num v2)) v1 v2) (error "MUPL max applied to non-number"))) )... ) CSE 413 Midterm, May 6, 2011 Sample Solution Page 7 of 8

Question 7. (5 points) The classic mark-sweep garbage collector for Scheme examines the heap to discover which cons cells are currently reachable, then it reclaims all of the unreachable data by adding it to a list of available cells to be reused later. The copying garbage collector discussed in class also discovers which memory cells are in use, but it copies all of the active memory cells to new locations in memory, updating pointers to the moved cells so they correctly point to the new locations of the moved data. It seems like the copying collector does an awful lot of extra work compared to the simple mark-sweep version. Is there any advantage to doing this? If so, what is it? (Be brief) The main advantage is that the copying collector moves all active data so it is contiguous in memory. This reduces the memory footprint of the heap. Among other things, this should make more memory available for other programs, particularly in a paged virtual memory system. CSE 413 Midterm, May 6, 2011 Sample Solution Page 8 of 8