Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Similar documents
Homework 6. What To Turn In. Reading. Problems. Handout 15 CSCI 334: Spring, 2017

Homework 4. Notes. Turn-In Instructions. Reading. Self Check. Handout 16 CSCI 334: Spring 2018

by the evening of Tuesday, Feb 6

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

Exercises on ML. Programming Languages. Chanseok Oh

Scope, Functions, and Storage Management

ML Built-in Functions

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

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

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

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Homework 1. Reading. Problems. Handout 1 CS242: Autumn September

Functional Programming and Haskell

Box-and-arrow Diagrams

Introduction to OCaml

CS153: Compilers Lecture 15: Local Optimization

Control in Sequential Languages

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

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

Programming Languages

CSCI-GA Scripting Languages

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)

Lists. Michael P. Fourman. February 2, 2010

Chapter 3 Linear Structures: Lists

CS 320 Homework One Due 2/5 11:59pm

CSE 341 Section 5. Winter 2018

Functional Programming - 2. Higher Order Functions

Programming Languages

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

CS558 Programming Languages

Programming Language Concepts, CS2104 Lecture 7

Programming Languages

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Problem Set 1 Due: 11:59pm Wednesday, February 7

Standard ML. Curried Functions. ML Curried Functions.1

Handout 2 August 25, 2008

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

CS558 Programming Languages

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

Implementing nml: Hindley-Milner Type Inference

Chapter 3 Linear Structures: Lists

CSci 4223 Principles of Programming Languages

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CMSC330 Fall 2014 Midterm 2 Solutions

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

Draw a diagram of an empty circular queue and describe it to the reader.

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

Patterns The Essence of Functional Programming

Lecture 19: Functions, Types and Data Structures in Haskell

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Types and Type Inference

CS422 - Programming Language Design

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Polymorphism and Type Inference

Lambda Calculus and Type Inference

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

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

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

Overloading, Type Classes, and Algebraic Datatypes

15 150: Principles of Functional Programming. More about Higher-Order Functions

Programming Languages

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

COMP 105 Assignment: Hindley-Milner Type Inference

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Programming Languages Lecture 14: Sum, Product, Recursive Types

April 2 to April 4, 2018

CSE341 Spring 2017, Final Examination June 8, 2017

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Part I: Written Problems

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

CSE3322 Programming Languages and Implementation

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

Haskell Overview II (2A) Young Won Lim 9/26/16

Types, Type Inference and Unification

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

CMSC 330, Fall 2013, Practice Problems 3

CS 11 Ocaml track: lecture 2

Lecture 10 September 11, 2017

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

CSE3322 Programming Languages and Implementation

CSE341, Spring 2013, Final Examination June 13, 2013

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

COMPUTER SCIENCE TRIPOS

Variables and Bindings

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

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

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Transcription:

Homework 5 Due Wednesday, 14 March Handout 19 CSCI 334: Spring 2018 Notes This homework has two types of problems: Problems: You will turn in answers to these questions. Pair Programming: This part involves writing ML code. You are required to work with a partner for this section. I will assume that you plan to work with the same partner you worked with on the last assignment unless you email me (dbarowy@cs.williams.edu) with your partner s name by the evening of Wednesday, March 7. Turn-In Instructions You must turn in your work for this assignment in two parts, one for the Problems part and one for the Pair Programming part. You will be assigned two GitHub repositories. Note that this assignment should be anonymized: neither your name nor your partner s name should appear in any of the files submitted with this assignment except the file collaborators.txt (see below). Problems: Problem sets should be typed up using L A TEX and submitted using your cs334 hw5 <username> repository. For example, if your GitHub username is dbarowy, then your repository will be called cs334 hw5 dbarowy. Be sure that your work is committed and pushed to your repository by Wednesday, March 14 at 11:59pm. If you discuss the problem set with your partner or with a study group, please be sure to include their names in a collaborators.txt file in your repository. Pair Programming: Programming solutions should be typed up and submitted using your partner repository. For example, if your GitHub username is dbarowy and you are working with a partner whose username is wjannen, look for a repository called cs334 hw5 dbarowy-wjannen (usernames will be in alphabetical order). Be sure that your work is committed and pushed to your repository by Wednesday, March 14 at 11:59pm. Standard ML files should have a.sml suffix. For example, the pair programming problem P1a should appear as the file p1a.sml. Your code should be documented using comments. Comment lines appear inside (* and *) braces. Reading 1. (Required) Read Mitchell, Chapters 6 and 7. Problems Q1. (25 points)................................................................. Infer the Types Infer the ML types for the following function using the Hindley-Milner algorithm. fun f (x::xs) = (x,x)::(f xs) f nil = nil 1

Specifically, (a) convert the function to a λ expression; (b) draw the corresponding parse tree for the λ expression; (c) using a table, label each subexpression with a type variable; (d) generate constraints using the λ-calculus constraint rules; (e) and solve the constraints, yielding the type of f. Note that this problem includes a case not discussed in class, but which you can find in Mitchell pp. 135 145. Assume that the cons operator :: is a curried function of type a a list a list. You may also assume that the tuple constructor (a,b) is a curried function of type a b a* b. Q2. (10 points)............................................. Static and Dynamic Scope Consider the following program fragment, written in ML: 1 let val x = 2 in 2 let val f = fn y => x + y in 3 let val x = 7 in 4 x + 5 f x 6 end 7 end 8 end; (a) Under static scoping, what is the value of x + f x in this code? During the execution of this code, the value of x is needed three different times (on lines 2, 4, and 5). For each line where x is used, state what numeric value is used when the value of x is requested and explain why these are the appropriate values under static scoping. (b) Under dynamic scoping, what is the value of x + f x in this code? For each line where x is used, state which value is used for x and explain why these are the appropriate values under dynamic scoping. Q3. (15 points)..................... Function Calls and Memory Management This question asks about memory management in the evaluation of the following staticallyscoped ML expression. val x = 5; fun f(y) = (x+y)-2; fun g(h) = let val x = 7 in h(x) end; let val x = 10 in g(f) end; (a) (12 points) Fill in the missing information in the following depiction of the run-time stack after the call to h inside the body of g. Remember that function values are represented by closures, and that a closure is a pair consisting of an environment (pointer to an activation record) and compiled code. In this drawing, a bullet ( ) indicates that a pointer should be drawn from this slot to the appropriate closure or compiled code. Since the pointers to activation records cross and could become difficult to read, each activation record is numbered at the far left. In each activation record, place the number of the activation record of the statically enclosing scope in the slot labeled access link. The first two are done for you. Also use activation record numbers for the environment pointer part of each closure pair. Write the values of local variables and function parameters directly in the activation records. 2

Activation Records Closures Compiled Code (1) access link ( 0 ) x (2) access link ( 1 ) f (3) access link ( ) ( ), g code for f (4) access link ( ) ( ), x (5) g(f) access link ( ) h code for g x...... (6) h(x) access link ( ) y (b) (3 points) What is the value of this expression? Why? Pair Programming P1. (5 points)................................................................ Lisp Conditional A Lisp programmer learning ML doesn t like the syntax of the ML conditional expression, if...then...else. Instead, the programmer decides to define a conditional function: fun Cond(test, truecase, falsecase) = if test then truecase else falsecase; You foresee problems if this programmer uses this Cond function exclusively in place of if...then...else. Write two short programs that demonstrate the problem. (a) Write a short program using Cond. (b) Write the supposedly equivalent program using if...then...else that produces a different result. P2. (10 points).............................................................. Exceptions in ML The function stringtonum defined below uses two auxiliary functions to convert a string of digits into a non-negative integer. (* Convert one character to a numeric digit. *) fun chartonum c = ord c - ord #"0"; fun calclist (nil,n) = n calclist (fst::rest,n) = calclist(rest,10 * n + chartonum fst); (* Convert a string of digits to a number. The explode function converts a string to a list of characters. *) fun stringtonum s = calclist(explode s, 0); For instance, stringtonum "3405" returns the integer 3405. (The function explode converts a string into a list of characters, and ord returns the ASCII integer value for a character.) 3

Unfortunately, calclist returns a spurious result if the string contains any non-digits. For instance, stringtonum "3a05" returns 7905, while stringtonum " 405" returns 15595. This occurs because chartonum will convert any character, not just digits. We can attempt to fix this by having chartonum raise an exception if it is applied to a non-digit. (a) Revise the definition of chartonum to raise an exception, and then modify the function stringtonum so that it handles the exception, returning 1 if there is a non-digit in the string. You should make no changes to calclist. (b) Implement ML functions to provide the same behavior (including returning 1 if the string includes a non-digit) as in the first part, but without using exceptions. While you may change any function, try to preserve as much of the structure of the original program as possible. (c) Which implementation do you prefer? Why? Answer this as a comment in the code. P3. (20 points)...................................................................... Folding Fun Use foldl or foldr to solve the following problems. (a) Write a function concatwords: string list -> string. This function should return return a string with all strings in the list concatenated: - concatwords nil; val it = "" : string - concatwords ["Three", "Short", "Words"]; val it = "ThreeShortWords" : string (b) Write a function words length: string list -> int. This function should return the total length of all words appearing in a list of strings. For example: - words_length nil; val it = 0 : int - words_length ["Three", "Short", "Words"]; val it = 15 : int (c) Can we always use foldl in place of foldr? If yes, explain. If no, give an example function f, list l, and initial value v such that foldr f v l and foldl f v l behave differently. (d) Write a function count: a -> a list -> int. It computes the number of times a value appears in a list. For example: - count "sheep" ["cow", "sheep", "sheep", "goat"]; val it = 2 : int - count 4 [1,2,3,4,1,2,3,4,1,2,3,4]; val it = 3 : int (e) Write a function partition: int -> int list -> int list * int list that takes an integer p and a list of integers l, and that returns a pair of lists containing the elements of l smaller than p and those greater than or equal to p. The ordering of the original list should be preserved in the returned lists. (We wrote a recursive form during lecture as part of quicksort.) - partition 10 [1,4,55,2,44,55,22,1,3,3]; val it = ([1,4,2,1,3,3],[55,44,55,22]) : int list * int list (f) Write a function poly: real list -> (real -> real) that takes a list of reals [a 0, a 1,..., a n 1 ] and returns a function that takes an argument b and evaluates the polynomial a 0 + a 1 x + a 2 x 2 + + a n 1 x n 1 at x = b; that is, it computes n 1 i=0 a ib i. For example, 4

- val g = poly [1.0, 2.0]; val it = fn: real -> real - g(3.0); val it = 7.0: real - val g = poly [1.0, 2.0, 3.0]; val it = fn: real -> real - g(2.0); val it = 17.0: real (Hint: a 0 + a 1 x + a 2 x 2 + a 3 x 3 = a 0 + x(a 1 + x(a 2 + xa 3 )). This is an example of Horner s Rule. Horner s Rule demonstrates that we can evaluate a degree n polynomial with only O(n) multiplies.) 5