CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2017, Midterm Examination April 28, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

Part I: Written Problems

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

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

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

CSE3322 Programming Languages and Implementation

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

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

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

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

CSci 4223 Principles of Programming Languages

CSE351 Winter 2016, Final Examination March 16, 2016

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

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

Sample Exam; Solutions

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

CSE373 Winter 2014, Midterm Examination January 29, 2014

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

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

CSE 130, Fall 2005: Final Examination

CSE 130, Fall 2006: Final Examination

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

Lists. Michael P. Fourman. February 2, 2010

Problem Score Max Score 1 Syntax directed translation & type

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSCI-GA Final Exam

CSc 372, Spring 1996 Mid-term Examination Solution Key

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

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

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

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

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

CPSC 311, 2010W1 Midterm Exam #2

ML Built-in Functions

CSE 341: Programming Languages

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE351 Spring 2018, Midterm Exam April 27, 2018

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)

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

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

CSE351 Spring 2018, Final Exam June 6, 2018

A quick introduction to SML

CSE 341 : Programming Languages

CIS 120 Midterm I February 16, 2015 SOLUTIONS

15 210: Parallel and Sequential Data Structures and Algorithms

CSE331 Spring 2015, Final Examination June 8, 2015

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

CSE-321 Programming Languages 2012 Midterm

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

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSE-321 Programming Languages 2010 Midterm

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

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

Fall 2018 Lecture N

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

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

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

Consider the following EBNF definition of a small language:

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

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

Patterns The Essence of Functional Programming

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

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

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

SNU Programming Language Theory

Higher-Order Functions

CS 415 Midterm Exam Spring 2002

CSE 130 [Winter 2014] Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

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? (a) fun check_pipe p = case p of Straight i => i > 0 Curve (i,r) => i > 0 andalso r > 0.0 andalso r < 2.0 * Math.pi Tee(p1,p2,p3) => check_pipe p1 andalso check_pipe p2 andalso check_pipe p3 Sequence(p1,p2) => check_pipe p1 andalso check_pipe p2 (b) fun scale_model (p,s) = case p of Straight i => Straight (i*s) Curve(i,r) => Curve(i*s,r) Tee(p1,p2,p3) => Tee(scale_model (p1,s), scale_model (p2,s), scale_model (p3,s)) Sequence(p1,p2) => Sequence(scale_model (p1,s), scale_model (p2,s)) (c) i. Sequence (Straight 7,Curve (9,1.5)) ii. Sequence (Straight 70,Curve (90,1.5))

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? (a) There are many solutions, including any 3 of the following: ([],[1,2,3,4]) ([1],[2,3,4]) ([1,3],[2,4]) ([1,3,4],[2]) ([1,2,3,4],[]) (b) No (c) ( a list * a list) -> a list (d) i. B ii. A iii. B iv. A

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))) (a) 5 (b) 18 (c) 12

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? (a) fun nonempty_for_x x flist = case flist of [] => [] f::flist => let val s = f x in if s = "" then nonempty_for_x x flist else s :: nonempty_for_x x flist (b) A few ways: fun nonempty_for_x x = List.filter (fn s => String.size s > 0) o List.map (fn f => f x) fun nonempty_for_x x = List.filter (fn s => s <> "") o List.map (fn f => f x) fun nonempty_for_x x = List.filter (fn s => not (s = "") o List.map (fn f => f x) (c) Yes, a -> ( a -> string) list -> string list (d) Yes, a -> ( a -> string) list -> string list

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. (a) [35,36] (b) [36,35] (c) One returns the reverse of the list the other returns.

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.) (a) yes (b) yes (c) yes (d) yes (e) i. yes ii. 5 (f) i. no ii. 5 (g) int * int -> int (h) a * b -> b

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.