Sample Exam; Solutions

Similar documents
Lists. Michael P. Fourman. February 2, 2010

Principles of Functional Programming

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2016, Midterm Examination April 29, 2016

Name CPTR246 Spring '17 (100 total points) Exam 3

CSE 341 Section 5. Winter 2018

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

Inductive Data Types

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

Specification, Implementation, and Use

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

A Brief Introduction to Standard ML

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Balanced Search Trees

A quick introduction to SML

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Programming Languages and Techniques (CIS120)

CSE341 Spring 2017, Midterm Examination April 28, 2017

15 210: Parallel and Sequential Data Structures and Algorithms

Chapter 6 Abstract Datatypes

ITI Introduction to Computing II

ITI Introduction to Computing II

CSE3322 Programming Languages and Implementation

Lecture 19: Signatures, Structures, and Type Abstraction

CSC/MAT-220: Lab 6. Due: 11/26/2018

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

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

CS 216 Exam 1 Fall SOLUTION

CSE 341 : Programming Languages Midterm, Spring 2015

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

Homework 5 ML module language assigned Thursday 20 March, due Friday 4 April

1 Ordered Sets and Tables

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Products and Records

Standard ML. Data types. ML Datatypes.1

Chapter 20: Binary Trees

CS4120/4121/5120/5121 Spring 2018 Xi Type System Specification Cornell University Version of February 18, 2018

CSE 143 Final Part 1, August 18, 2011 Sample Solution

Lecture 3: Recursion; Structural Induction

CS115 - Module 8 - Binary trees

The type checker will complain that the two branches have different types, one is string and the other is int

UNIVERSITY REGULATIONS

CS558 Programming Languages

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

Consider the following EBNF definition of a small language:

CSL 201 Data Structures Mid-Semester Exam minutes

School of Computer Science

Name: Entry: Gp: 1. CSL 102: Introduction to Computer Science. Minor 2 Mon 21 Mar 2005 VI 301(Gps 1-6)& VI 401(Gps 7-8) 9:30-10:30 Max Marks 40

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.

CSE341, Fall 2011, Midterm Examination October 31, 2011

CS3110 Spring 2016 Lecture 6: Modules

CSE-321 Programming Languages 2012 Midterm

Begin at the beginning

F28PL1 Programming Languages. Lecture 14: Standard ML 4

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

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Priority queues. Priority queues. Priority queue operations

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Variables and Bindings

G Programming Languages - Fall 2012

Programming Languages and Techniques (CIS120)

Typed Scheme: Scheme with Static Types

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

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Programming Languages

Higher-Order Functions

Linked Structures. See Section 3.2 of the text.

Trees. Eric McCreath

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

Programming Languages and Techniques (CIS120)

INF2220: algorithms and data structures Series 1

CSE-321 Programming Languages 2010 Midterm

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

CMSC351 - Fall 2014, Homework #2

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

Lecture Notes on Induction and Recursion

CS171 Final Practice Exam

Lecture 31: Graph Search & Game 10:00 AM, Nov 16, 2018

Data and File Structures Laboratory

Standard ML. Exceptions

Programming in Standard ML: Continued

Section 05: Solutions

CSE373 Fall 2013, Midterm Examination October 18, 2013

Matriculation number:

Examination Questions Midterm 1

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

Fall Lecture 8 Stephen Brookes

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

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

CSE 143, Winter 2009 Sample Midterm Exam #2

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Transcription:

Sample Exam; Solutions Michael P. Fourman February 2, 2010 1 Introduction This document contains solutions to the sample questions given in Lecture Note 10 Short Question 5 marks Give the responses of the ML system to the following sequence of declarations val a = 1; val b = 2; fun f a = a + b; val b = 3; f b; the responses of the system are as follows: val a = 1 : int val b = 2 : int val f = fn : int -> int val b = 3 : int val it = 5 : int The point here is to check that you understand, and can apply, the scoping rules, applied to the bindings of a and b. The exam on Thursday may include let and local declarations, in a similar question. 1

1. Long Question 10 marks The following datatype can be used to represent trees whose nodes can have an arbitrary number of children. datatype a Tree = Tree of a * a Tree list (a) What tree does the following expression denote (i.e draw a picture): Tree(1, [Tree(2, [ ]), Tree(3, [Tree(4,[ ])])]) (b) Define a function to calculate the number of leaves in such a tree. fun sum [] = 0 sum (h :: t) = h + sum t fun leaves (Tree(_,[])) = 1 leaves (Tree(_,ts)) = sum (map leaves ts) (c) We can assign a level to each node in a tree as follows. The node at the root is at level 1. Its children are at level 2. Their children are at level 3 and so on. Suppose we are interested in trees where an internal node at level n always has exactly n children. Define a function check : a Tree ->bool that checks whether a given tree has this property. The recursion is not straightforward: to check the property for a tree, we must check a slightly different property for its subtrees. We therefor introduce an auxiliary function, checkk, with an extra parameter; checkk k checks that the appropriate property holds for a subtree rooted at level k: fun length [] = 0 length (_::t) = 1 + length t fun andl [] = true (* and over a list of booleans *) andl (h :: t) = h andalso andl t fun checkk k (Tree(_,[])) = true (* nothing to check for a leaf * checkk k (Tree(_,ts)) = ((length ts) = k) 2

fun check t = checkk 1 t (* check there are k children *) andalso andl (map (checkk (k+1)) ts) (* subtrees are at level (k+1) *) 3

2. Long Question 10 marks The EQueue signature is like the signature Queue, but is extended with an additional operation multiple enqueue, menq:(item list * Queue) -> Queue, intended to add a number of items (in an arbitrary order) to the queue in a single operation. signature EQueue = sig type Item type Queue end val empty : Queue val enq : (Item * Queue) -> Queue val deq : Queue -> (Item * Queue) val menq: (Item list * Queue) -> Queue An implementation of a stack, including this operation, uses the type declaration type Queue = Item list list the operations empty and menq are implemented as follows: val empty = [] fun menq(items, q) = items :: q (a) Complete the following declarations of the functions enq and deq for this implementation fun enq(item, []) = [[item]] enq(item, (h :: t)) = (item :: h) :: t (* or, alternatively, [item] :: h :: t *) fun deq((h :: t) :: r) = (h, t :: r) deq([] :: r) = deq r deq [] = raise Deq The point here is to take care with the types. Since a stack is being represented as a list of lists, we need to make a list, [[item]], whose only member is the singleton list, [item], to represent a stack with one entry. When adding an item to a non-empty stack, we have a choice: we can either add the item to the list at the 4

head of the list of lists, or we can form a new singleton list and add this to the list of lists. (b) What is the complexity of the three operations i. enq, O(1) ii. deq, O(1) iii. menq O(1) for this implementation? Notice that, for a conventional stack implementation we would have to implement menq using multiple calls of enq. The complexity would be O(n), where n is the number of items being added in one go. 3. Long Question 10 marks An implementation of sets of integers is designed to represent a set by a list without repetitions, kept in increasing order. Here is the function union : Set*Set -> Set from this implementation fun union(a, []) = a union([], b) = b union(ah :: at, bh :: bt) = if ah < bh then ah :: union(at, bh :: bt) else if ah = bh then ah :: union(at, bt) else bh :: union(ah :: at, bt) (a) What is the complexity of this implementation of union? O(n), where n is the sum of the sizes of the sets; there is at most one recursive call for each of these elements. (b) Give an implementation of the operation insert : (int*set) ->Set compatible with this representation. fun insert (x,[]) = [x] insert (x,h :: t) = if x < h then x :: h :: t else if h < x then h :: insert(x,t) else (* x = h *) h :: t This is book-work: a similar definition was given in the notes to implement a priority queue. (c) Give an O(n) implementation of the operation intersect : Set*Set -> Set, compatible with this representation. 5

fun intersect(a, []) = [] intersect([], b) = [] intersect(ah :: at, bh :: bt) = if ah < bh then intersect(at, bh :: bt) else if ah = bh then ah :: intersect(at, bt) else intersect(ah :: at, bt) This follows the pattern given in the declaration of union. (C) Michael Fourman 1994-2006 6