CS 312, Fall Exam 1

Similar documents
Computer Science 312 Fall Prelim 2 SOLUTIONS April 18, 2006

CSE341, Fall 2011, Midterm Examination October 31, 2011

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

About coding style Spring February 9, 2004

Next. Tail Recursion: Factorial. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial.

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Autumn 2017, Midterm Examination October 30, 2017

7.3.3 A Language With Nested Procedure Declarations

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

Today. Recap from last Week. Factorial. Factorial. How does it execute? How does it execute? More on recursion. Higher-order functions

CS 312 Problem Set 1: An Introduction to SML

Mini-ML. CS 502 Lecture 2 8/28/08

CSci 4223 Principles of Programming Languages

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

Final Exam CSCI 1101 Computer Science I KEY. Wednesday December 16, 2015 Instructor Muller Boston College. Fall 2015

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

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

An Explicit-Continuation Metacircular Evaluator

Programming Languages

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

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

CSE341 Spring 2016, Midterm Examination April 29, 2016

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

CSE 341 : Programming Languages Midterm, Spring 2015

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

Fall Lecture 3 September 4. Stephen Brookes

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

Structure and Interpretation of Computer Programs

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

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

Second Examination Solution

CSE 341 Section 5. Winter 2018

CMSC 330, Fall 2013, Practice Problems 3

CS150: Exam 2 Due: Mon Apr 20th by 3:30pm (in class) or by 6:30pm (OLS 219) UVA ID: Directions

15-451/651: Design & Analysis of Algorithms November 20, 2018 Lecture #23: Closest Pairs last changed: November 13, 2018

Supplementary Notes on Concurrent ML

CSE-321 Programming Languages 2012 Midterm

Lecture 3: Recursion; Structural Induction

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CSE 130 [Winter 2014] Programming Languages

FUNCTIONAL PEARLS Drawing Trees

Second Exam CS 1101 Computer Science I Spring Section 03 KEY. Thursday April 14, Instructor Muller Boston College

CS 340 Spring 2019 Midterm Exam

A Crash Course on Standard ML

cs173: Programming Languages Midterm Exam

CIS 500 Software Foundations Midterm I

COP-5555 PROGRAMMING LANGUAGEPRINCIPLES NOTES ON RPAL

CSc 372, Fall 1996 Mid-Term Examination Monday, October 21, 1996 READ THIS FIRST

COMP 105 Assignment: Hindley-Milner Type Inference

Cornell University 12 Oct Solutions. (a) [9 pts] For each of the 3 functions below, pick an appropriate type for it from the list below.

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

Structure and Interpretation of Computer Programs

CSci 4223 Principles of Programming Languages

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

Introduction to OCaml

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

COT 3100 Spring 2010 Midterm 2

CSE505, Fall 2012, Midterm Examination October 30, 2012

Functions Over Lists. Programming Languages and Compilers (CS 421) Structural Recursion. Functions Over Lists. Structural Recursion : List Example


DO NOT OPEN UNTIL INSTRUCTED

15-451/651: Design & Analysis of Algorithms April 18, 2016 Lecture #25 Closest Pairs last changed: April 18, 2016

A Brief Introduction to Standard ML

University of Illinois at Chicago Department of Computer Science. Final Examination. CS 151 Mathematical Foundations of Computer Science Fall 2012

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

CSE 341 : Programming Languages

CS 275 Name Final Exam Solutions December 16, 2016

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

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

CS1 Recitation. Week 2

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

CSE341 Spring 2017, Final Examination June 8, 2017

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CSE341, Spring 2013, Final Examination June 13, 2013

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

G Programming Languages - Fall 2012

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

CMSC330 Spring 2017 Midterm 2

CSE 130, Fall 2005: Final Examination

Programming Languages

Implementing nml: Hindley-Milner Type Inference

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

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

An example DFA: reaching definitions

Problem Score Max Score 1 Syntax directed translation & type

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

Version 4, 12 October 2004 Project handed out on 12 October. Complete Java implementation due on 2 November.

CSE 130, Fall 2006: Final Examination

CMSC 330 Exam #2 Fall 2008

Begin at the beginning

Transcription:

Name: ID number: CS 312, Fall 2003 Exam 1 October 16, 2003 Problem 1 2 3 4 Total Grader Grade 18 24 28 30

There are 4 problems on this exam. Please check now that you have a complete exam booklet with 9 numbered pages plus the cover sheet. Write your name or id number on each page of the exam. Be sure to try all of the problems, as some are more difficult than others (i.e., don t waste a lot of time on a problem that is givg you a hard time move on to another problem and then return to it later). The order of the problems is (very roughly) by creasg difficulty. This exam is closed book. No papers, books or notes may be used. There is space provided to answer each question. You may request extra paper if necessary. Do not put any answers on the backs of pages (THEY WILL NOT BE GRADED), ask for an extra piece of paper if you need more space. To help ensure that you don t accidentally miss any of the questions, we have marked those sections where an answer is requested with a the right marg. The staff reserves the right to ignore illegible answers. Pretty prtg (proper dentation) of your code will aid the gradg process. 1. (18 Pots) For each of the followg expressions determe what value would be returned if you started up SML and typed this expression to the terpreter. Alternatively, dicate that an error would occur. (Note that you should assume each expression is typed to a brand-new SML terpreter.) (a) let val x = 42 val y = [x,x] hd(y)+x Answer: 84 (b) let val y = [4,2] val f = fn(x) => x-4 val z = map f y hd(y) * hd(tl(z)) Answer: 8 1

(c) let val f = fn(x) => x-4 val x = 2 f(x)::[f(x)] Answer: [ 2, 2] (d) let val f = fn(x) => let val z = 2 fn(y) => (z*y)+x f(4)(2) Answer: 8 (e) let val f = fn(x) => x-z val x = 2 val z = 4 f(x)::[f(x)] Answer: Error (both x and z are unbound) (f) let val x = 42 val f = fn(x) => x+42 val x = 24 val g = fn(x) => f(x)+x f(g(x)) Answer: 132 2

2. (24 Pots) Some of the followg expressions can be made to evaluate to 42 by fillg TYPE and EXP correctly. For example, the expression let val HHGTTG: TYPE = EXP HHGTTG(21) can be made to evaluate to 42 by havg TYPE = t->t and EXP = fn(x:t) => x*2. For each of the followg expressions, specify TYPE and EXP, or state that the expression cannot be made to evaluate to 42. Please note that for simplicity you may not use parameterized types or exceptions or exception handlers your solutions. (a) let val HHGTTG: TYPE = EXP foldl HHGTTG 1 [2,3,4] TYPE = (t*t)->t EXP = (fn(x:t,y:t)=>if x = 4 then 42 else x*y) (b) let val HHGTTG: TYPE = EXP let val ford = (fn(x) => map hd x) hd(ford(hhgttg)) + hd(tl(ford(hhgttg))) TYPE = t list list EXP = [[2,15],[40,14]] 3

(c) let val HHGTTG: TYPE = EXP val ford = fn(f,g) => fn(x) => f(g(x+1)*20) val arthur = ford (hd(hhgttg),hd(tl(hhgttg))) arthur(4) TYPE = (t->t) list EXP = [fn(x) => 42, fn(y) => y] (d) let val HHGTTG: TYPE = EXP val ford = fn(query) => if query(6*9) = 42 then 42 else raise Fail "not the answer!" val arthur = fn(question) => 42 val marv = fn(question) => 24 HHGTTG(ford(arthur),ford(marv)) Impossible, because functions evaluate their arguments. Full credit for statg that it is impossible because ford(marv) will raise an exception. 4

3. (28 pots) The procedure countdown takes two tegers m, n and returns a list of length m +1whoseelementsare[m, m 1,m 2,...,1,n]. So, for example, countdown(3,4) [3,2,1,4] countdown(0,42) [42] (a) (3 pots) Write a simple recursive defition of the function countdown. To simplify the next part of the problem, your defition should not be tail-recursive, and should not use pattern matchg or case (use if and list primitives like :: stead). Sample solution: fun countdown(m:t,n:t):t list = if (m = 0) then [n] else m::countdown(m-1,n) (b) (20 pots) Prove that your defition of the countdown function is correct, usg duction and the substitution model. You may assume that all arithmetic and list primitives referenced the defition of countdown perform correctly. To receive full credit you must justify each step. 1. Statement to be proved: countdown(m, n) = [m, m 1,...,1,n]. Wecould write this as P[m], sce the output recursively deps on m. 2. Induction variable: m 0, 1, 2... Note that you can t do duction on n here! 3. Base case: We need to prove P[0], i.e. countdown(0,n) = [n] By the RSM (substitution), countdown(0,n) = if 0 = 0 then [n] else 0::countdown(0-1,n) This is [n] by the RSM (semantics of =,if), so P[0] is true. 4. Induction case: we need to prove P[m] implies P[m + 1]. In other words, we need to show: countdown(m +1,n) = [m +1,m,m 1,...,1,n] assumg the Induction Hypothesis, which states that countdown(m, n) = [m, m 1,...,1,n] 5

By the RSM (substitution), we have: countdown(m +1,n) = if m +1 = 0 then [n] else m +1::countdown(m + 1-1,n) Sce m 0, 1, 2... we know that m +1 0. Applyg the RSM (semantics of =,if) weget: countdown(m +1,n) = m +1::countdown(m +1-1,n) which of course yields countdown(m +1, n) = m +1::countdown(m, n) By the Induction Hypothesis this gives us countdown(m +1,n) = m +1::[m, m 1,...,1,n] which by the RSM (semantics of ::) gives countdown(m +1,n) = [m +1,m,m 1,...,1,n] This is exactly what we wanted to prove. 6

(c) (5 pots) Write a tail-recursive defition of the function countdown. Sample solution: fun countdowniter(m:t,n:t):t list = let fun iter(m0:t, ans:t list) = if (m0 = 0) then List.rev(n::ans) else iter(m0-1,m0::ans) iter(m,[]) 7

4. (30 pots) Now consider the problem of determg if a list of tegers is the value of countdown(m, n) for two tegers m, n. You will need to write a function countdownv(l) that returns the m, n such that l = countdown(m, n). If there are no such m, n the value of countdownv(l) should be the value of the expression nocount() which we could defe as follows: fun nocount():(t*t) = raise Fail "No such countdown" After your code, the SML terpreter should behave as follows: -countdownv([42]); val it = (0,42) : t * t -countdownv(countdown(4,2)); val it = (4,2) : t * t -countdownv([42,42]); uncaught exception Fail: No such countdown You will write several versions of this function. (a) (10 pots) Write a non-recursive solution for countdownv. (Ht: you are welcome to defe and use helper functions.) Sample solution: fun countdownv(lst:t list):(t*t) = let val (a,b) = (List.length(lst)-1, hd(list.rev(lst))) if (null(lst) orelse countdown(a,b) <> lst) then nocount() else (a,b) There is also a solution usg fold. 8

(b) (10 pots) Write a recursive, but not tail-recursive, defition of countdownv. Sample solution: fun countdownv(lst:t list):(t*t) = case lst of [] => nocount() [last] => (0,last) x::y => let val (a,b) = countdownv(y) if (x = a+1) then (x,b) else nocount() (c) (10 pots) Write a tail-recursive defition of countdownv. Ht: if the expressions E1 and E2 do not volve the function iter, then a function of the form fun iter(lst:t list,x:t):(t*t) = if (x <> E1) then E2 else iter(tl(lst),x-1) will be tail-recursive. Sample solution: fun countdownv(lst:t list):(t*t) = case lst of [] => nocount() [last] => (0,last) start::y => let fun iter(rema:t list,next:t):(t*t) = case rema of [last] => if (next <> 0) then nocount() else (start,last) a::b => if (a <> next) then nocount() else iter(b,a-1) iter(y,start-1) 9