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

Size: px
Start display at page:

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

Transcription

1 Computer Science CSC324 Wednesday February 13, 2013 St. George Campus University of Toronto Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m. Silent Policy A silent policy takes effect 24 hours before this assignment is due. This means that no question about this assignment will be answered, whether it is asked on the forum, by , or in person. How you will be marked There are 100 marks available in this assignment. Your solutions will be marked with respect to correctness, style, efficiency, readability, and quality of documentation and testing. The file cover.txt contains the detailed breakdown of the marks. This assignment counts for 10% of the course grade. Handing in this Assignment For this assignment, you need to submit the following files: cover.txt evaluator.sml questions.txt tree.sml fix.sml cover.txt can be found in /u/csc324h/winter/pub/a3/. To submit a file named filename.ext electronically, execute the following command on cdf: submit -c csc324h -a a3 -f filename.ext Type man submit for more information. Warning: Marks will be deducted for incorrect submission. Note that without a properly completed and signed cover page (simply type your name in the Signature field), your assignment will not be marked. Warning: Your code must run in SML/NJ on CDF to be assigned a mark. No exceptions. Assignment 3 Announcements and Discussion Board Important clarifications to the assignment will be posted on the course webpage. You are also responsible for monitoring the CSC324 forum. Getting Started with SML 1. To start SML on CDF, just type sml. You will see the SML prompt -, and you can type in ML code and the interpreter immediately evaluates it and produces output. 2. To quit SML, simply press Ctrl-D (under Windows press Ctrl-Z). 3. To load your code from a file: - use "myfile.sml"; 4. To determine or change the current directory: - OS.FileSys.getDir() - OS.FileSys.chDir("path") 5. Comments in SML are specified as: (* comments start here *) 1

2 6. A very useful reference for understanding SML error messages is: ML Coding Guidelines Use Pattern Matching. The use of pattern matching, whenever possible, is considered good style. For example, rather than writing: fun sum L = if null L then 0 else hd L + sum (tl L); it is better to write: fun sum [] = 0 sum (h::t) = h + sum t; Documentation. Each function you write, including the helper functions, should have a concise and clear description of the function, the argument, preconditions, postconditions, return value, and the type of the function. Do not include the type in the preconditions, but provide it in a separate statement in the comment for the function. Any assumptions you make about the form of a function s inputs/outputs (other than their type) should be included in the preconditions. Style and Indentation. We should be able to tell the structure of your function by looking at it. Use indentation appropriately. In general, you should write clear and readable code (for which you will need to use your own judgement). Pick informative names for your helper functions and variables. For multiline comments, the following is considered good style: (* comments * comments * comments * comments *) Efficiency. Your code should be reasonably efficient (though not necessary optimal). For example, you should not evaluate the same expression multiple times, or traverse lists unnecessarily. Also, do not create unneeded variables or bind then unnecessarily. Question 1. Datatypes (Submitted Filename: evaluator.sml). For this question, you will write a mathematical expression evaluator for a simple calculator language. Your evaluator will take a program in the form of parsed abstract syntax, and execute it, producing an integer resulting from performing all of the mathematical operations (including applications of user-defined functions) designated by the program. You can assume that the users program has been parsed into an abstract syntax represented by the datatype Expr, as follows: type Name = string; datatype Expr = Const of int Var of Name 2

3 Neg of Expr Plus of Expr * Expr Mult of Expr * Expr App of Fun * Expr and Fun = Def of Name * Expr Each element of type Expr represents a parsed expression of our simple calculator language. An expression can be a constant integer, addition or multiplication of two expressions, the negation of an expression, or the application of a function to an expression. Functions are defined by a variable bound by the function and an expression representing the functions body. For example, the Scheme function (lambda (x) (* 2 (+ x 1))) would be represented as: Def("x", Mult(Const 2, Plus(Var "x", Const 1))) where the definition Def is simply a function of one variable, that binds this one variable (whose name is given as the first argumnet of the constructor Def) within the body of the definition (the expression that is given as the second argument of Def). In the subquestions below, you will write functions for evaluating expressions in our simple calculator language. a. Define a function substitute: (Name, Expr, Expr) -> Expr, that takes a tuple of three elements: n of type Name, e1 of type Expr, and e2 of type Expr. substitute returns a new expression, that is the result of replacing every occurrence of Var n in e2 by expression e1. for example: (* substitute every Var "x" in expression Var "x" by expression Var "y". *) - substitute ("x", Var "y", Var "x"); val it = Var "y" : expr (* substitute every Var "z" in expression Var "x" by expression Var "y". There is none. *) - substitute ("z", Var "y", Var "x"); val it = Var "x" : expr (* substitute every "z" in expression Const 1 by expression Var "y". There is none. *) - substitute ("z", Var "y", Const 1); val it = Const 1 : Expr (* substitute every Var "x" in expression Mult(Var "x", Const 3) by expression Const 2. *) - substitute ("x", Const 2, Mult(Var "x", Const3)); val it = Mult(Const 2, Const 3) : Expr (* substitute every Var "z" in expression Neg(Plus(Var "x", Var "z")) by expression Var "y". *) - substitute ("z", Var "y", Neg(Plus(Var "x", Var "z"))); val it = Neg(Plus(Var "x", Var "y")) : Expr (* substitute every Var "z" in expression App(Def("x", Plus(Var "z", Var "x")), Const 3) by expression Neg(Const 1). *) - substitute ("z", Neg(Const 1), App(Def("x", Plus(Var "z", Var "x")), Const 3); val it = App(Def("x", Plus(Neg(Const 1), Var "x")), Const 3) : Expr Note: You should assume the following preconditions for the function substitute(n, e1, e2): 3

4 1. n is not bound by any function-definition within e2. 2. No variable appearing in e1 is bound by a function-definition in e2. 3. Definitions in e2 have fresh variable names, that is: no name is bound by more than one definition in e2, and that no name bound by a definition appears outside the body of the definition. Thus, the following example is not a valid use of substitute because it results in an invalid expression, i.e., Def("x", Plus(Var "y", Var "z")). Note that this use of substitute actually violates the first precondition above. - substitute("x", Var "y", Def("x", Plus(Var "x", Var "z"))); b. Write an evaluator for closed expressions of type Expr(see below for a definition of closed expressions). You should write a function eval: Expr -> int that reduces a given closed expression to an integer by evaluating the expression. Constants cannot be further simplified/reduced, and the mathematical operators Plus, Mult, and Neg are evaluated in the obvious fashion, so that: - eval (Const 2); val it = 2 : int - eval (Plus ((Const 2), (Const 3))); val it = 5 : int - eval (Neg (Mult ((Const 2), (Const 3)))); val it = ~6 : int Function applications, App(f,e), are evaluated by evaluating the body of the applied function (f: Fun) after substituting every instance of the function s bound variable with the value of the second part of the function application. For example: - eval (App (Def ("x", Plus (Var "x", Const 2)), (Const 3))); val it = 5 : int (* Note: the initial expression is equal to eval (Plus (Const 3, Const 2)). *) - eval (App (Def ("x", Plus (Var "x", Var "x")), Plus (Const 12, Const 9))); val it = 42 : int (* Note: the initial expression is equal to eval (Plus (Plus (Const 12, Const 9), Plus (Const 12, Const 9))) - eval (App (Def ("x", App (Def ("y", Plus(Var "x", Var "y")), Var "x")), Const 3)); val it = 6 : int (* Note: the initial expression is equal to eval (App (Def ("y", Plus (Const 3, Var "y")), Const 3)), which is equal to: eval (Plus (Const 3, Const 3)) *) A closed expression is one that does not contain variables that are not mentioned in the function definitions within the expression. If an expression is not closed, then your evaluator function should raise an exception, EvalException. For example, evaluating the following should raise an exception: 4

5 - eval (Var "x"); - eval (Plus (Var "x", Const 3)); - eval (App (Def ("y", Plus(Var "y", Var "x")), Const 3)); Question 2. Answering Questions (Submitted Filename questions.txt). Notice that our tiny programming language in Question (1) is not completely functional, in the sense that functions cannot be passed as parameters to functions, or returned from functions. Answer each of the following questions: a. What about the datatype Expr prevents returning functions as values? b. Briefly state how we can fix this problem. c. Write a new datatype declaration NewExpr in which functions are first-class expressions that may be returned from functions and passed as arguments to functions. Do not worry about enforcing correctness of expressions in the datatype declaration (i.e., What happens if I try to add two functions?, or What happens if I try to apply something that isn t a function, etc.). These are what a type-checker would do if we had one for our tiny language. Hint: You should only have to add one line to Expr and change one line already in Expr. Question 3. Recursive Datatypes; Higher-Order Functions (Submitted Filename: tree.sml). Consider the following datatype: datatype a tree = Node of a * a tree list; that represents a polymorphic tree, in which each node can have any number of children. In other words, each node in tree has a label (that can be of any type but real), followed by a list of its children, each representing a tree itself. For example, the following are instances of an int tree: Node(1, []) representing a single node with the label 1 and no children. Node(2, [ Node(1, []), Node(3, []) ]) representing a node (with label 2) and two children: one a node with the label 1, and the other a node with the lable 3. Node(6, [ Node(2, [ Node(1, []), Node(3, []) ]), Node(4, [ Node(5, []), Node(7, []) ]) ]) representing the following visually-represented tree: 6 / \ 2 4 / \ / \

6 In the subquestions below, you will write functions for peforming certain operations on a tree. a. Write a function count: a * a tree -> int that takes an element x and a tree t, and returns the number of times that x appears as a node label in t. Do not use higher-order procedures (HOPs). Use recursion. b. Now write the same function as in 3(a), but using HOPs. Call this counthop. c. Write a function depth: a tree -> int, which takes a tree and returns its depth: i.e., the maximum length of a path from the root of the tree to a leaf. Do not use HOPs. Use recursion. d. Now write the same function as in 3(c) using HOPs. Call this depthhop. Question 4. Functions as Return Values; Currying (Submitted Filename: fix.sml). We call x a fixed point of function f if f(x) = x. In the following subquestions, you will write variations of a function fix: ( a -> a) -> ( a -> a) that takes a unary function f as input, and returns a function that applies f to its argument repeatedly until a fixed point is reached, i.e., until ((f x) = x). Note that, the functions returned by fix may not terminate for all inputs. For example, (fix (fn x => ~x)) never reaches a fixed point if we apply it to anything other than 0. You should not worry about such cases (assume they are supposed to run forever!). Here are some examples of two unary functions (mytl and halve), and their use with fix: fun mytl [] = [] mytl (h::r) = r fun halve x = x div 2; - (fix mytl) []; val it = []:?.X1 list - (fix mytl) [1, 2, 3]; val it = [] : int list - (fix halve) 2; val it = 0 : int - (fix halve) ~10; val it = ~1 : int a. Name your function fixa, and write it using a globally-defined helper function. fixa should not be recursive, and the helper function should not return a function. b. Name your function fixb, and write it using recursion and no helper function. Note that fixb is recursive, and that each recursive call returns an unnamed function expression. c. Name your function fixc, and write it using no helper functions and no unnamed functions. You will need to write fixc using a curried function definition of the following form: fun fixc f x =... 6

A Brief Introduction to Standard ML

A Brief Introduction to Standard ML A Brief Introduction to Standard ML Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität

More information

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

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter Due: Thurs October 27, 10:00pm CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter Note: This is a much longer assignment than anything we ve seen up until now. You are given two weeks to complete

More information

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

A Concepts-Focused Introduction to Functional Programming Using Standard ML Sample Exam #1 Draft of August 12, 2010 A Concepts-Focused Introduction to Functional Programming Using Standard ML Sample Exam #1 Draft of August 12, 2010 Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece

More information

CSE 341, Spring 2011, Final Examination 9 June 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. CSE 341, Spring 2011, Final Examination 9 June 2011 Please do not turn the page until everyone is ready. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper.

More information

Part I: Written Problems

Part I: Written Problems CSci 4223 Homework 3 DUE: Friday, March 22, 11:59 pm Instructions. Your task is to answer 2 written problems, and to write 16 SML functions (excluding local helper functions) as well as test cases for

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language

More information

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

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides CSC324- TUTORIAL 5 ML Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides Assignment 1 2 More questions were added Questions regarding the assignment? Starting ML Who am I? Shems Saleh

More information

Recap: Functions as first-class values

Recap: Functions as first-class values Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse

More information

A quick introduction to SML

A quick introduction to SML A quick introduction to SML CMSC 15300 April 9, 2004 1 Introduction Standard ML (SML) is a functional language (or higherorder language) and we will use it in this course to illustrate some of the important

More information

Programming Languages

Programming Languages CSE 130 : Winter 2009 Programming Languages News PA 2 out, and due Mon 1/26 5pm Lecture 5: Functions and Datatypes t UC San Diego Recap: Environments Phone book Variables = names Values = phone number

More information

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

CSC/MAT-220: Lab 6. Due: 11/26/2018 CSC/MAT-220: Lab 6 Due: 11/26/2018 In Lab 2 we discussed value and type bindings. Recall, value bindings bind a value to a variable and are intended to be static for the life of a program. Type bindings

More information

Begin at the beginning

Begin at the beginning Begin at the beginning Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of

More information

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

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

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

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes CSE 130 : Fall 2007 Programming Languages News PA deadlines shifted PA #2 now due 10/24 (next Wed) Lecture 5: Functions and Datatypes Ranjit Jhala UC San Diego Recap: Environments Phone book Variables

More information

CSC324 Functional Programming Typing, Exceptions in ML

CSC324 Functional Programming Typing, Exceptions in ML CSC324 Functional Programming Typing, Exceptions in ML Afsaneh Fazly 1 Winter 2013 1 with many thanks to Anya Tafliovich, Gerald Penn, Sheila McIlraith, Wael Aboelsaddat, Tony Bonner, Eric Joanis, Suzanne

More information

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught

More information

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

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources Scope and Introduction to Functional Languages Prof. Evan Chang Meeting 7, CSCI 3155, Fall 2009 Announcements Assignment 3 due Thu at 11:55pm Submit in pairs Website has SML resources Text: Harper, Programming

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

L3 Programming September 19, OCaml Cheatsheet

L3 Programming September 19, OCaml Cheatsheet OCaml Cheatsheet Note: this document comes from a previous course (by Sylvain Schimdt). The explanations of the OCaml syntax in this sheet are by no means intended to be complete or even sufficient; check

More information

CSCI-GA Final Exam

CSCI-GA Final Exam CSCI-GA 2110-003 - Final Exam Instructor: Thomas Wies Name: Sample Solution ID: You have 110 minutes time. There are 7 assignments and you can reach 110 points in total. You can solve the exercises directly

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

Functional programming Primer I

Functional programming Primer I Functional programming Primer I COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Characteristics of functional programming Primary notions: functions and expressions (not

More information

Specification and Verification in Higher Order Logic

Specification and Verification in Higher Order Logic Specification and Verification in Higher Order Logic Prof. Dr. K. Madlener 13. April 2011 Prof. Dr. K. Madlener: Specification and Verification in Higher Order Logic 1 Chapter 1 Functional Programming:

More information

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Autumn 2017, Final Examination December 12, 2017 CSE341 Autumn 2017, Final Examination December 12, 2017 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

Variables and Bindings

Variables and Bindings Net: Variables Variables and Bindings Q: How to use variables in ML? Q: How to assign to a variable? # let = 2+2;; val : int = 4 let = e;; Bind the value of epression e to the variable Variables and Bindings

More information

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

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference. CS 3110 Spring 2015 Problem Set 3 Version 0 (last modified March 12, 2015) Soft deadline: 3/26/15 11:59pm Hard deadline: 3/28/15 1:00pm Overview In this assignment you will implement several functions

More information

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341, Spring 2013, Final Examination June 13, 2013 CSE341, Spring 2013, Final Examination June 13, 2013 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, except for both sides of one 8.5x11in piece of paper. Please stop

More information

COMP 105 Homework: Type Systems

COMP 105 Homework: Type Systems Due Tuesday, March 29, at 11:59 PM (updated) The purpose of this assignment is to help you learn about type systems. Setup Make a clone of the book code: git clone linux.cs.tufts.edu:/comp/105/build-prove-compare

More information

Flang typechecker Due: February 27, 2015

Flang typechecker Due: February 27, 2015 CMSC 22610 Winter 2015 Implementation of Computer Languages I Flang typechecker Due: February 27, 2015 Project 3 February 9, 2015 1 Introduction The third project is to implement a type checker for Flang,

More information

Assignment 4: Evaluation in the E Machine

Assignment 4: Evaluation in the E Machine Assignment 4: Evaluation in the E Machine 15-312: Foundations of Programming Languages Daniel Spoonhower (spoons@cs.cmu.edu) Edited by Jason Reed (jcreed@cs.cmu.edu) Out: Thursday, September 30, 2004 Due:

More information

CSE341 Spring 2016, Final Examination June 6, 2016

CSE341 Spring 2016, Final Examination June 6, 2016 CSE341 Spring 2016, Final Examination June 6, 2016 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function CSE 130 : Fall 2011 Recap from last time Programming Languages Lecture 3: Data Types Ranjit Jhala UC San Diego 1 2 A shorthand for function binding Put it together: a filter function # let neg = fun f

More information

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

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

Assignment 1. University of Toronto, CSC384 - Introduction to Artificial Intelligence, Winter

Assignment 1. University of Toronto, CSC384 - Introduction to Artificial Intelligence, Winter Assignment 1. University of Toronto, CSC384 - Introduction to Artificial Intelligence, Winter 2014 1 Computer Science 384 January 26, 2014 St. George Campus University of Toronto Homework Assignment #1

More information

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

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018 CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018 Typical workflow concrete syntax (string) "(fn x => x + x) 4" Parsing Possible errors / warnings

More information

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

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

Programming Languages

Programming Languages CSE 130 : Winter 2013 Programming Languages Lecture 3: Crash Course, Datatypes Ranjit Jhala UC San Diego 1 Story So Far... Simple Expressions Branches Let-Bindings... Today: Finish Crash Course Datatypes

More information

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

Mini-ML. CS 502 Lecture 2 8/28/08 Mini-ML CS 502 Lecture 2 8/28/08 ML This course focuses on compilation techniques for functional languages Programs expressed in Standard ML Mini-ML (the source language) is an expressive core subset of

More information

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

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More CSE341 Section 3 Standard-Library Docs, First-Class Functions, & More Adapted from slides by Daniel Snitkovskiy, Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman Agenda 1. SML Docs Standard

More information

Lecture 3: Recursion; Structural Induction

Lecture 3: Recursion; Structural Induction 15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion

More information

Handout 2 August 25, 2008

Handout 2 August 25, 2008 CS 502: Compiling and Programming Systems Handout 2 August 25, 2008 Project The project you will implement will be a subset of Standard ML called Mini-ML. While Mini- ML shares strong syntactic and semantic

More information

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

The type checker will complain that the two branches have different types, one is string and the other is int 1 Intro to ML 1.1 Basic types Need ; after expression - 42 = ; val it = 42 : int - 7+1; val it = 8 : int Can reference it - it+2; val it = 10 : int - if it > 100 then "big" else "small"; val it = "small"

More information

Currying fun f x y = expression;

Currying fun f x y = expression; Currying ML chooses the most general (least-restrictive) type possible for user-defined functions. Functions are first-class objects, as in Scheme. The function definition fun f x y = expression; defines

More information

CSE 341 Section 5. Winter 2018

CSE 341 Section 5. Winter 2018 CSE 341 Section 5 Winter 2018 Midterm Review! Variable Bindings, Shadowing, Let Expressions Boolean, Comparison and Arithmetic Operations Equality Types Types, Datatypes, Type synonyms Tuples, Records

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

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

Homework 6. What To Turn In. Reading. Problems. Handout 15 CSCI 334: Spring, 2017 Homework 6 Due 11 April Handout 15 CSCI 334: Spring, 2017 What To Turn In Please hand in work in two pieces, one for the Problems and one for the Programming (Partner Optional): Problems: Turn in handwritten

More information

Programming Assignment 2

Programming Assignment 2 CS 122 Fall, 2004 Programming Assignment 2 New Mexico Tech Department of Computer Science Programming Assignment 2 CS122 Algorithms and Data Structures Due 11:00AM, Wednesday, October 13th, 2004 Objectives:

More information

COMP 105 Assignment: Hindley-Milner Type Inference

COMP 105 Assignment: Hindley-Milner Type Inference COMP 105 Assignment: Hindley-Milner Type Inference Due Monday, Novermber 21 at 11:59PM. In this assignment you will implement Hindley-Milner type inference, which represents the current ``best practice''

More information

Metaprogramming assignment 3

Metaprogramming assignment 3 Metaprogramming assignment 3 Optimising embedded languages Due at noon on Thursday 29th November 2018 This exercise uses the BER MetaOCaml compiler, which you can install via opam. The end of this document

More information

Fall Lecture 3 September 4. Stephen Brookes

Fall Lecture 3 September 4. Stephen Brookes 15-150 Fall 2018 Lecture 3 September 4 Stephen Brookes Today A brief remark about equality types Using patterns Specifying what a function does equality in ML e1 = e2 Only for expressions whose type is

More information

Homework 4 Due Friday, 10/3/08

Homework 4 Due Friday, 10/3/08 Homework 4 Due Friday, 10/3/08 Please turn in your homework solutions by the beginning of class to the dropoff folder on vpn.cs.pomona.edu in directory /common/cs/cs131/dropbox. Make sure that all of your

More information

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

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 7 First-Class Functions Dan Grossman Winter 2013 What is functional programming? Functional programming can mean a few different things: 1. Avoiding mutation in most/all

More information

More Assigned Reading and Exercises on Syntax (for Exam 2)

More Assigned Reading and Exercises on Syntax (for Exam 2) More Assigned Reading and Exercises on Syntax (for Exam 2) 1. Read sections 2.3 (Lexical Syntax) and 2.4 (Context-Free Grammars) on pp. 33 41 of Sethi. 2. Read section 2.6 (Variants of Grammars) on pp.

More information

(Refer Slide Time: 4:00)

(Refer Slide Time: 4:00) Principles of Programming Languages Dr. S. Arun Kumar Department of Computer Science & Engineering Indian Institute of Technology, Delhi Lecture - 38 Meanings Let us look at abstracts namely functional

More information

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm) CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, 2015 11:30pm) This program focuses on binary trees and recursion. Turn in the following files using the link on the

More information

Project 1: Scheme Pretty-Printer

Project 1: Scheme Pretty-Printer Project 1: Scheme Pretty-Printer CSC 4101, Fall 2017 Due: 7 October 2017 For this programming assignment, you will implement a pretty-printer for a subset of Scheme in either C++ or Java. The code should

More information

Sample Exam; Solutions

Sample Exam; Solutions 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

More information

Implementing nml: Hindley-Milner Type Inference

Implementing nml: Hindley-Milner Type Inference Implementing nml: Hindley-Milner Type Inference Due Friday, April 10 at 5:59PM. In this assignment you will implement Hindley-Milner type inference, which represents the current ``best practice'' for flexible

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 52/37/19/6 (2:30,3:35,11:15,7:30) PS2 due Thursday 9/22 11:59PM Quiz #1 back in section Monday Quiz #2 at start of class on Thursday 9/22 o HOP s, and lots

More information

An Introduction to Functions

An Introduction to Functions Chapter 4 An Introduction to Functions Through the agency of with, we have added identifiers and the ability to name expressions to the language. Much of the time, though, simply being able to name an

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

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

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Part I: Written Problems

Part I: Written Problems CSci 4223 Homework 1 DUE: Friday, February 1, 11:59 pm Instructions. Your task is to answer three written problems, and to write eleven SML functions related to calendar dates, as well as test cases for

More information

Programming Languages

Programming Languages CSE 130 : Spring 2011 Programming Languages Lecture 3: Crash Course Ctd, Expressions and Types Ranjit Jhala UC San Diego A shorthand for function binding # let neg = fun f -> fun x -> not (f x); # let

More information

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions Recap Epressions (Synta) Compile-time Static Eec-time Dynamic Types (Semantics) Recap Integers: +,-,* floats: +,-,* Booleans: =,

More information

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember. CSE 130 Programming Languages Recap: ML s Holy Trinity Expressions (Syntax) Exec-time Dynamic Values (Semantics) Datatypes Compile-time Static Types Ranjit Jhala UC San Diego 1. Programmer enters expression

More information

Programming Languages

Programming Languages CSE 130 : Fall 2008 Programming Languages Lecture 2: A Crash Course in ML Ranjit Jhala UC San Diego News On webpage: Suggested HW #1, sample for Quiz #1 on Thu PA #1 (due next Fri 10/10) No make-up quizzes

More information

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego CSE 130 Programming Languages Datatypes Ranjit Jhala UC San Diego Recap: ML s Holy Trinity Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression

More information

CS 312 Problem Set 5: Concurrent Language Interpreter

CS 312 Problem Set 5: Concurrent Language Interpreter CS 312 Problem Set 5: Concurrent Language Interpreter Due: 11:00 PM, April 14, 2005 1 Introduction In this assignment you will build an interpreter for a funtional language called CL, with concurrency

More information

CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

Recap: ML s Holy Trinity

Recap: ML s Holy Trinity Recap: ML s Holy Trinity Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages. Programming Language Paradigm CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

More information

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points Files to submit: 1. HW3.py This is a PAIR PROGRAMMING Assignment: Work with your partner! For pair

More information

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

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually Plan (next 4 weeks) 1. Fast forward Rapid introduction to what s in OCaml 2. Rewind 3. Slow motion Go over the pieces individually History, Variants Meta Language Designed by Robin Milner @ Edinburgh Language

More information

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

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu. UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm Sample s CSC324H1 Duration: 50 minutes Instructor(s): David Liu. No Aids Allowed Name: Student Number: Please read the following guidelines carefully.

More information

Compilers Project 3: Semantic Analyzer

Compilers Project 3: Semantic Analyzer Compilers Project 3: Semantic Analyzer CSE 40243 Due April 11, 2006 Updated March 14, 2006 Overview Your compiler is halfway done. It now can both recognize individual elements of the language (scan) and

More information

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

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

Lecture 2: SML Basics

Lecture 2: SML Basics 15-150 Lecture 2: SML Basics Lecture by Dan Licata January 19, 2012 I d like to start off by talking about someone named Alfred North Whitehead. With someone named Bertrand Russell, Whitehead wrote Principia

More information

Inductive Data Types

Inductive Data Types Inductive Data Types Lars-Henrik Eriksson Functional Programming 1 Original slides by Tjark Weber Lars-Henrik Eriksson (UU) Inductive Data Types 1 / 42 Inductive Data Types Today Today New names for old

More information

CSCI 3155: Homework Assignment 3

CSCI 3155: Homework Assignment 3 CSCI 3155: Homework Assignment 3 Spring 2012: Due Monday, February 27, 2012 Like last time, find a partner. You will work on this assignment in pairs. However, note that each student needs to submit a

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 6A Spring 203 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,

More information

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

15 150: Principles of Functional Programming. More about Higher-Order Functions 15 150: Principles of Functional Programming More about Higher-Order Functions Michael Erdmann Spring 2018 1 Topics Currying and uncurrying Staging computation Partial evaluation Combinators 2 Currying

More information

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong. Data Abstraction An Abstraction for Inductive Data Types Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Introduction This lecture

More information

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a

More information

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23 Plan Part 1 (Preliminaries):

More information

Repetition Through Recursion

Repetition Through Recursion Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages

More information

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged

More information

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

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm. CS52 - Assignment 8 Due Friday 4/15 at 5:00pm https://xkcd.com/859/ This assignment is about scanning, parsing, and evaluating. It is a sneak peak into how programming languages are designed, compiled,

More information

Module 8: Local and functional abstraction

Module 8: Local and functional abstraction Module 8: Local and functional abstraction Readings: HtDP, Intermezzo 3 (Section 18); Sections 19-23. We will cover material on functional abstraction in a somewhat different order than the text. We will

More information

Introduction to SML Getting Started

Introduction to SML Getting Started Introduction to SML Getting Started Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen, Fall 2004 p.1/15 Background Standard Meta

More information

A general introduction to Functional Programming using Haskell

A general introduction to Functional Programming using Haskell A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell

More information