CSE341 Spring 2016, Midterm Examination April 29, 2016

Similar documents
CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2017, Midterm Examination April 28, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE 341 : Programming Languages Midterm, Spring 2015

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

CSE341, Spring 2013, Final Examination June 13, 2013

CSE 341 Section 5. Winter 2018

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2016, Final Examination June 6, 2016

CSE341 Autumn 2017, Final Examination December 12, 2017

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

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

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE3322 Programming Languages and Implementation

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

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

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

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

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

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

Part I: Written Problems

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

CSE351 Winter 2016, Final Examination March 16, 2016

CSci 4223 Principles of Programming Languages

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

Sample Exam; Solutions

CSE373 Winter 2014, Midterm Examination January 29, 2014

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

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

CSE 130, Fall 2005: Final Examination

CSE 130, Fall 2006: Final Examination

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSCI-GA Final Exam

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Lists. Michael P. Fourman. February 2, 2010

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

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Problem Score Max Score 1 Syntax directed translation & type

CSc 372, Spring 1996 Mid-term Examination Solution Key

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

ML Built-in Functions

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE351 Spring 2018, Midterm Exam April 27, 2018

CIS 120 Midterm I October 2, Name (printed): Username (login id):

SML Style Guide. Last Revised: 31st August 2011

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 341: Programming Languages

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

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CSE351 Spring 2018, Final Exam June 6, 2018

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CPSC 311, 2010W1 Midterm Exam #2

CSE331 Spring 2015, Final Examination June 8, 2015

15 210: Parallel and Sequential Data Structures and Algorithms

Preliminary Examination I Computer Science 312, Cornell University 6 March 2003

A quick introduction to SML

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

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

Standard ML. Data types. ML Datatypes.1

Midterm I - Solution CS164, Spring 2014

CSE3322 Programming Languages and Implementation

Programming Languages

CSE-321 Programming Languages 2010 Midterm

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

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

CSE-321 Programming Languages 2012 Midterm

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Fall 2018 Lecture N

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

CS 320 Midterm Exam. Fall 2018

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

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

CSE3322 Programming Languages and Implementation

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

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

CS 415 Midterm Exam Spring 2002

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

CSE 130 [Winter 2014] Programming Languages

Higher-Order Functions

Programming Languages and Techniques (CIS120)

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

UNIVERSITY OF TORONTO Faculty of Arts and Science

SNU Programming Language Theory

Inductive Data Types

Consider the following EBNF definition of a small language:

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

Transcription:

CSE341 Spring 2016, Midterm Examination April 29, 2016 Please do not turn the page until 10:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please stop promptly at 11:20. There are 100 points, distributed unevenly among 6 questions (all with multiple parts): The exam is printed double-sided. Advice: Read questions carefully. Understand a question before you start writing. Write down thoughts and intermediate steps so you can get partial credit. But clearly indicate what is your final answer. The questions are not necessarily in order of difficulty. Skip around. Make sure you get to all the questions. If you have questions, ask. Relax. You are here to learn.

1. (24 points) This problem uses this datatype binding, where a value of type pipe describes the shape of a pipe system (e.g., for carrying water). datatype pipe = Straight of int Curve of int * real Tee of pipe * pipe * pipe Sequence of pipe * pipe Straight i represents a straight pipe of length i centimeters. Curve (i,r) represents a curved pipe of length i centimeters with an arc of r radians, meaning the curve occupies r/(2π) of a circle. Tee (p1,p2,p3) is a tee (also known as a fork?) that connects the three pipes together, with p2 and p3 being in a line that is at a right angle to p1. Sequence (p1,p2) connects the two pipes together. (a) Write a function check_pipe of type pipe -> bool that returns true if and only if all lengths anywhere in the argument are positive and all arcs in curves are strictly between 0 and 2π. (You can use Math.pi, which has type real.) (b) Write a function scale_model of type pipe * int -> pipe that creates a pipe of the same shape as the first input but with all lengths scaled (multiplied) by the second input. (Arcs stay the same.) (c) Consider this code that uses your answer to part (b); val little_p = Sequence (Straight (3+4), Curve (4+5, 1.5)) val big_p = scale_model (little_p, 10) i. What value is bound to little_p? ii. What value is bound to big_p?

2. (17 points) This problem uses this ML code: fun foo (xs,ys) = case (xs,ys) of ([],[]) => [] (* branch 1 *) ([],_) => ys (* branch 2 *) (_,[]) => xs (* branch 3 *) (x::xs,_) => x::(foo(ys,xs )) (* branch 4 *) (a) Give three different inputs to foo that all lead to the output [1,2,3,4]. Each of your answers should already be a value (i.e., not contain other expressions like addition or function calls). (b) Is foo tail-recursive? (c) What is the type of foo? (d) For each of the following, give exactly one of these answers: A. It leads to a match nonexhaustive warning. B. It leads to no warning and the resulting function is equivalent to foo (the branch was unnecessary). C. It leads to no warning but the resulting function is not equivalent. i. What happens if we remove just branch 1 (and, for parsing purposes, the character that follows)? ii. What happens if we remove just branch 2? iii. What happens if we remove just branch 3? iv. What happens if we remove just branch 4?

3. (12 points) For each of the following programs, give the value ans is bound to after evaluation. (a) val c = 12 fun f a = let val b = a - 1 val a = b - 1 val b = a - 1 in c - b val c = 10 val ans = f c (b) fun f p = let val q = p 1 val r = q 2 in (r 3) + (p 0 0 0) fun g x = let val y = 6 in f (fn z => fn w => fn t => z + w + t + y) val ans = g 7 (c) exception E fun h a = case a of NONE => raise E SOME a => a val a = 12 val ans = h (h (SOME (SOME a)))

4. (20 points) (a) Without using any helper functions (except :: and =), write a function nonempty_for_x of type int -> ((int -> string) list) -> (string list) as follows: It takes two arguments x and flist in curried form. The output list contains no empty strings (i.e., ""). The i th element of the output list is the i th non-empty string produced by calling each element of flist in order with x. Hint: You can see if a string is empty by comparing it to "" using =. (b) Create a function nonempty_for_x that is equivalent to nonempty_for_x by filling in these blanks with anonymous functions: fun nonempty_for_x x = (List.filter ) o (List.map ) (c) Does your nonempty_for_x actually have a more general type than the type specified? If so, what is it? (d) Does your nonempty_for_x actually have a more general type than the type specified? If so, what is it?

5. (9 points) (a) What is x bound to after this ML code evaluates? val x = List.filter (fn i => i > 32 andalso i < 39) [0,99,35,36,14] (b) What is y bound to after this ML code evaluates? fun filterish f xs = List.foldl (fn (i,acc) => if f i then i::acc else acc) [] xs val y = filterish (fn i => i > 32 andalso i < 39) [0,99,35,36,14] (c) In approximately one English sentence, explain the general difference between List.filter and filterish.

6. (18 points) This problem considers two ML structures and two ML signatures, all related to intervals (also known as ranges) of integers where we consider a range like 3 to 7 to include both points. signature INTERVAL1 = sig type t = int * int val make : int * int -> t val contains : t * int -> bool val size : t -> int signature INTERVAL2 = sig type t val make : int * int -> t val contains : t * int -> bool val size : t -> int structure IntervalA = struct type t = int * int fun make (x,y) = (Int.min(x,y), Int.max(x,y)) fun contains ((x,y),i) = x <= i andalso i <= y fun size (x,y) = y - x structure IntervalB = struct type t = int * int fun make (x,y) = (Int.min(x,y), abs (y - x)) fun contains ((x,len),i) = x <= i andalso i <= x + len fun size (_,len) = len (a) Does IntervalA have signature INTERVAL1 (i.e., would structure IntervalA :> INTERVAL1... typecheck)? (b) Does IntervalA have signature INTERVAL2 (i.e., would structure IntervalA :> INTERVAL2... typecheck)? (c) Does IntervalB have signature INTERVAL1 (i.e., would structure IntervalB :> INTERVAL1... typecheck)? (d) Does IntervalB have signature INTERVAL2 (i.e., would structure IntervalB :> INTERVAL2... typecheck)? (e) Suppose a program has two structures S1 and S2 both with signature INTERVAL1. Further suppose S1 s make is the same as in IntervalA and S2 s size is the same as in IntervalB. i. Would S2.size (S1.make (5,~5)) type-check? ii. Regardless of whether it type-checks, if we assume we can evaluate it, what would S2.size (S1.make (5,~5)) evaluate to? (f) Repeat the previous question assuming S1 and S2 both have signature INTERVAL2. (g) What is the type of size inside IntervalA? (Do not use type t in your answer.) (h) What is the type of size inside IntervalB? (Do not use type t in your answer.)

Here is an extra page in case you need it. If you use it for a question, please write see also extra sheet or similar on the page with the question.