CSE341, Spring 2013, Final Examination June 13, 2013

Similar documents
CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2016, Final Examination June 6, 2016

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

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

CSE 341 : Programming Languages Midterm, Spring 2015

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

A Concepts-Focused Introduction to Functional Programming Using Standard ML Sample Exam #1 Draft of August 12, 2010

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

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

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

CSE 505, Fall 2007, Final Examination 10 December Please do not turn the page until everyone is ready.

CSE 505, Fall 2006, Final Examination 11 December Please do not turn the page until everyone is ready.

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

Comp 311: Sample Midterm Examination

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

CSE351 Winter 2016, Final Examination March 16, 2016

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CS 415 Midterm Exam Spring 2002

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

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

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

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

CSE331 Spring 2015, Final Examination June 8, 2015

Functions & First Class Function Values

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

CSE413 Midterm. Question Max Points Total 100

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Functional Programming. Pure Functional Programming

CSE 505, Fall 2009, Final Examination 14 December Please do not turn the page until everyone is ready.

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS 275 Name Final Exam Solutions December 16, 2016

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

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

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

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSEP505 Programming Languages, Autumn 2016, Final Exam December, Programming Languages for a World of Change

Variables and Bindings

CPSC 311, 2010W1 Midterm Exam #2

Lexical vs. Dynamic Scope

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CSCI-GA Scripting Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSE373 Winter 2014, Midterm Examination January 29, 2014

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

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

CSE 130, Fall 2005: Final Examination

Recap: Functions as first-class values

CSE 341, Autumn 2015, Ruby Introduction Summary

From Syntactic Sugar to the Syntactic Meth Lab:

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

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

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

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

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

CSE351 Spring 2018, Final Exam June 6, 2018

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

CSE 130, Fall 2006: Final Examination

Lexical Addresses. let x = 1 y = 2 in let f = proc (x) +(x, y) in (f x) let _ = 1 _ = 2 in let _ = proc (_) +(<0,0>, <1,1>) in (<0,0> <1,0>)

cs173: Programming Languages Midterm Exam

Introduction to Scheme

CSE 341 Section 5. Winter 2018

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Box-and-arrow Diagrams

CS 314 Principles of Programming Languages

CS 360 Programming Languages Interpreters

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 26 Course Victory Lap. Dan Grossman Spring 2016

Writing Interpreters Racket eval. CSE341 Section 7. ASTs, Interpreters, MUPL. Sunjay Cauligi. February 21 st, Sunjay Cauligi CSE341 Section 7

CSE341: Programming Languages Winter 2018 Unit 5 Summary Zach Tatlock, University of Washington

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

CMSC 330: Organization of Programming Languages. Operational Semantics

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

CSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.

Transcription:

CSE341, Spring 2013, Final Examination June 13, 2013 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, except for both sides of one 8.5x11in piece of paper. Please stop promptly at 10:20. You can rip apart the pages, but please staple them back together before you leave. There are 100 points total, distributed unevenly among 8 questions (many with multiple parts). When writing code, style matters, but do not worry much about indentation. Advice: Read questions carefully. Understand a question before you start writing. Write down thoughts and intermediate steps so you can get partial credit. The questions are not necessarily in order of difficulty. Skip around. Make sure you get to all the problems. If you have questions, ask. Relax. You are here to learn.

1. (16 points) (a) Without using any helper functions, write a Racket function filter-increasing, which works as follows: It takes three arguments (recall Racket supports multi-argument functions directly): (1) a function f that takes list elements and, we assume, returns numbers, (2) a number i, and (3) a list xs. It returns a list that contains a subset of the elements in xs in the same order they appear in xs. An element of xs is in the output if and only if f applied to the element produces a number greater than i and greater than the number produced by f for all elements earlier (closer to the head) in the list. (b) Write a Racket function filter-function-maker that takes two arguments that are the same as f and i in part (a) and returns a function that takes a list xs and returns the result of filter-increasing with f, i, and xs. Use your answer to part (a) as appropriate. (c) Write a call to filter-function-maker that returns a function that works as follows: It filters out any list element that is not a positive number. It includes any positive numbers that are greater than previous positive numbers in the list. For example, the result for (list 0 3 2 5 (list 19 24) #f 7 3) would be (3 5 7).

2. (11 points) For each of the following programs, indicate what would be printed. Notice that in each program there are two print expressions and there are two calls to the function. (a) (define (f1 x) (begin (print x) (set! x (+ x 7)) (print x))) (f1 12) (f1 12) (b) (define f2 (let ([x 5]) (lambda (a) (begin (print x) (set! x (+ x 7)) (print x))))) (f2 12) (f2 12) (c) (define f3 (let ([x 5]) (begin (print x) (lambda (a) (begin (set! x (+ x 7)) (print x)))))) (f3 12) (f3 12)

3. (13 points) Write a Racket function some-multiple-of-n that works as follows: It takes a stream s and a number n. (Remember a stream is a thunk that returns a pair where the cdr is a stream.) It returns a list. The length of the list will be k times n for some k 1. The returned list contains elements from the stream in order. It always contains the first n elements. If the n th element is #f, then the list contains the #f and another n elements. This continues until the first k for which the k n th stream element is not #f, in which case this stream element is in the returned list as the last element. Hint: You will need a recursive helper function that takes either two or three arguments, depending on how you organize your code.

4. (14 points) Here is the skeleton of the MUPL language and interpreter, including all the struct definitions that define the syntax of the language: (struct var (string) #:transparent) ;; a variable, e.g., (var "foo") (struct int (num) #:transparent) ;; a constant number, e.g., (int 17) (struct add (e1 e2) #:transparent) ;; add two expressions (struct ifgreater (e1 e2 e3 e4) #:transparent) ;; if e1 > e2 then e3 else e4 (struct fun (nameopt formal body) #:transparent) ;; a recursive(?) 1-argument function (struct call (funexp actual) #:transparent) ;; function call (struct mlet (var e body) #:transparent) ;; a local binding (let var = e in body) (struct apair (e1 e2) #:transparent) ;; make a new pair (struct fst (e) #:transparent) ;; get first part of a pair (struct snd (e) #:transparent) ;; get second part of a pair (struct aunit () #:transparent) ;; unit value -- good for ending a list (struct isaunit (e) #:transparent) ;; evaluate to 1 if e is unit else 0 (struct closure (env fun) #:transparent) (define (envlookup env str)...) (define (eval-under-env e env)...) (define (eval-exp e) (eval-under-env e null)) Below are four wrong calls to the MUPL interpreter. For each, identify which of these choices best describes what goes wrong and explain your answer in 1-3 English sentences by explaining what happens when the program runs. (There is room on the next page for your answers.) A. Racket will raise an error before p is defined, so eval-exp will never get called. B. eval-exp will get called, but with something that is not a legal MUPL program. C. eval-exp will get called with a legal MUPL program, but evaluation will encounter a MUPL dynamic type-error. (a) (define p (isaunit (ifgreater (int 1) (aunit) (int 3) (aunit)))) (eval-exp p) (b) (define p (isaunit? (ifgreater (int 1) (aunit) (int 3) (aunit)))) (eval-exp p) (c) (define p (aunit (ifgreater (int 1) (aunit) (int 3) (aunit)))) (eval-exp p) (d) (define p (aunit? (ifgreater (int 1) (aunit) (int 3) (aunit)))) (eval-exp p)

Extra room for answers to Problem 4.

5. (12 points) In this problem, we assume the purpose of the Java type system is to prevent method missing errors at run-time. We consider what would happen if we removed interfaces from the language, i.e., the language still had classes but there is simply no notion of interfaces or classes implementing them. For each answer below, explain your answer in 1 3 English sentences. (a) Would this revised language have a sound type system? (b) Would this revised language have a complete type system? (c) Are interfaces enough like Ruby mixins that implementing something like Ruby s Comparable or Enumerable in Java is easier with interfaces in the language than without? (d) If we added multiple inheritance at the same time we removed interfaces, how could we encode the same concept as interfaces?

6. (6 points) Write a Ruby method called curry that takes one argument and works as follows: We assume the argument is an instance of Proc with a call method that expects two arguments. It returns an instance of Proc that is a curried version of the argument: it takes one argument and returns another Proc. For example, this use of curry should assign 16 to v3: v1 = curry (lambda { a,b a + b}) v2 = v1.call 7 v3 = v2.call 9

7. (15 points) Consider the following silly ML code: datatype the_type = A of string B of int C of int * int fun f x = case x of A s => s ^ " is coming!" _ => "" fun g (x,y) = case x of B i => i + y C(i,j) => i + j + y A _ => y val foo = (f(a "summer"), g(a "winter",7)) (a) What is foo bound to after this program is evaluated? (b) Port this code to Ruby as follows: Use an OOP style with multiple class definitions. Do not define methods outside of these classes. Use initialize methods, other methods, and instance variables as appropriate. Have foo hold a two-element array. (There is more room on the next page in case you need it.)

Extra room for answers to Problem 7.

8. (13 points) In this problem, we consider a language like in lecture containing (1) records with mutable fields, (2) higher-order functions, and (3) subtyping. We do not require explanations for your answers. (a) For each of the following questions, answer yes if and only if the proposed subtyping relationship is sound, meaning it would not allow a program to type-check that could then try to access a field in a record that did not have that field. i. Is {f1 : string, f2 : string} a subtype of {f1 : string, f2 : string, f3 : string}? ii. Is {f1 : string, f2: {g1 : string, g2 : string} } a subtype of {f1 : string, f2 : {g1 : string} }? iii. Is string -> {f1 : string, f2 : int, f3 : int} a subtype of string -> {f3 : int, f2 : int}? iv. Is {f1 : string, f2 : int, f3 : int} -> string a subtype of {f3 : int, f2 : int} -> string? v. Is int -> {f1 : string, f2 : {g1 : string} } a subtype of int -> {f1 : string, f2 : {g1 : string, g2 : string} }? (b) If we change the language so that records are immutable (you cannot update contents of a field), which, if any, of your answers to part (a) change?