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

Similar documents
CSCC24 Functional Programming Typing, Scope, Exceptions ML

CSC324 Functional Programming Typing, Exceptions in ML

Inductive Data Types

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

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

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

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

A Brief Introduction to Standard ML

An introduction introduction to functional functional programming programming using usin Haskell

Exercises on ML. Programming Languages. Chanseok Oh

Standard ML. Data types. ML Datatypes.1

Chapter 3 Linear Structures: Lists

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

Functional Paradigm II

CSE341 Spring 2016, Midterm Examination April 29, 2016

Chapter 3 Linear Structures: Lists

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

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

Handout 2 August 25, 2008

A quick introduction to SML

CSE 341 Section 5. Winter 2018

Programming Languages

CMSC 330: Organization of Programming Languages

Lists. Michael P. Fourman. February 2, 2010

CSCI-GA Scripting Languages

CSE3322 Programming Languages and Implementation

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

Introduction to OCaml

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

Functional Paradigm II

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

CSci 4223 Principles of Programming Languages

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

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

Products and Records

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Programming Languages

Functional Programming

CSE 341: Programming Languages

CS 321 Programming Languages

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

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

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

Introduction to OCaml

CSCI-GA Final Exam

Lecture #23: Conversion and Type Inference

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

CSE 307: Principles of Programming Languages

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

CSE 307: Principles of Programming Languages

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Datatype declarations

Lecture 19: Functions, Types and Data Structures in Haskell

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

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Data Types

F28PL1 Programming Languages. Lecture 12: Standard ML 2

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

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

CS558 Programming Languages

Recap: ML s Holy Trinity

CSE341, Fall 2011, Midterm Examination October 31, 2011

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

CSC324 Principles of Programming Languages

CSE 130 [Winter 2014] Programming Languages

Programming in Standard ML: Continued

Part I: Written Problems

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

Standard ML. Exceptions

Functional programming Primer I

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

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

Lecture 2: The Basis of SML

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Functional Programming

02157 Functional Programming Tagged values and Higher-order list functions

CSC324 Principles of Programming Languages

CS558 Programming Languages

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

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

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

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

CSc 372, Spring 1996 Mid-term Examination Solution Key

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

CSE 3302 Programming Languages Lecture 8: Functional Programming

Note that pcall can be implemented using futures. That is, instead of. we can use

Begin at the beginning

Specification and Verification in Higher Order Logic

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

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

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

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)

CS 11 Ocaml track: lecture 2

Programming Languages and Compilers (CS 421)

Programming Languages

Transcription:

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 shems.saleh@mail.utoronto.ca What will we do today? SML Terminal use Instructions on use found here: http://www.cs.toronto.edu/~bonner/courses/2014f/csc324/handouts/usingml.txt Useful Tools ssh Putty, Filezilla (PC), terminal, Cyberduck/Filezilla (Mac), Sublime: SFTP/FTP package control ML currying, pattern recognition, more on types (userdefined/enumerated/variant/recursive), Let and exceptions Questions?

CURRYING

Currying in ML fun sum x y = x + y; is short form for: fun sum x = (fn y => x + y) So its type is: val sum = fn: int -> int -> int Note: this means int -> (int -> int), i.e. sum takes an int and returns a function of type int -> int. i.e. sum 2 would return a function

Back to functions for a quick note fun and fn are both short for function but with different meaning: fun is used for function declaration bind identifier to function Fn is used to introduce a value of type function.

Back to currying Compare sum1 and sum2: fun sum1 x y = x + y; val sum1 = fn: int -> int -> int fun sum2(x,y) = x + y; val sum2 = fn: int * int -> int What s the difference? Currying allows us to use partially instantiated functions

PATTERN MATCHING

Pattern Matching - val mytuple = ("foo", "bar"); val mytuple = ("foo","bar") : string * string - val (x,y) = mytuple; val x = "foo" : string val y = "bar" : string - val mylist = [1,2,3,4]; val mylist = [1,2,3,4] : int list -val h::r = mylist; val h = 1 : int val r = [2,3,4] : int list

Pattern Matching _ is don t care matches everything, binds nothing Examples: - val (first, _) = mytuple; val first = "foo" : string - val h::_ = [1,2,3]; val h = 1 : int

Pattern Matching Write the following function using pattern matching: fun len lst = if (null lst) then 0 else 1 + len (tl lst);

Pattern Matching Solution fun len [] = 0 len (x::xs) = 1 + len xs;

Pattern Matching Note If you didn t consider all the cases in your pattern matching function, you ll get a nonexhaustive exception Example: - fun len (x::xs) = 1 + len xs; stdin... Warning: match nonexhaustive x :: xs =>... val len = fn : 'a list -> int

Exercise Write a function firstlist that takes a list of pairs, and returns a new list consisting of the rst elements of all pairs. For example: firstlist [] ==> [] firstlist [(1,2),(1,3)] ==> [1,1] firstlist [(1,"a"),(2,"b"),(3,"c")] ==> [1,2,3] firstlist [([],"a"),([1],"b"),([1,2],"c")] ==> [[],[1],[1,2]]

Solution fun firstlist [] = [] firstlist ((e1,e2)::es) = e1::(firstlist es);

Last Note Regarding Pattern Matching Since ML is typed, the following 2 rules must always be followed: All patterns must have the same type All return values must have the same type

QUESTIONS?

USING LET IN ML

Let Expression Syntax let statements in body-expression end

Let Example fun reverse lst = let fun reverseacc [] acc = acc reverseacc (x::xs) acc = reverseacc xs (x::acc) in reverseacc lst [] end; reverse : 'a list -> 'a list - reverse [1,2,3]; val it = [3,2,1] : int list

TYPING

Type synonyms We can give existing types new names: Examples -type float = real; type float = real -type count = int and average = real; type count = int type average = real -val f : float = 2.3; val f = 2.3: float -val i = 3 : count; val i = 3: count

User-Defined Datatypes Syntax datatype type = constructor of pre-defined type constructor2 of pre-defined type 2 Type is omitted if constructor is constant (doesn t need arguments

Type Examples -datatype fruit = Apple Orange Banana; datatype fruit = Apple Orange Banana - val c = Apple; Val c = Apple : fruit fun fruitstr Apple = apple Orange = orange Banana = banana ; Note this is an enumerated type

Variant types You can create a datatype that contains multiple different pre-defined types within it Example datatype number = R of real I of int; val n1 = I 2; val n2 = R 3.0; val lst = [R 2.2, I 3, I 4, R 0.1]; (* val lst = [R 2.2,I 3,I 4,R 0.1] : number list *)

Exercise datatype number = R of real I of int; val lst = [R 2.2, I 3, I 4, R 0.1]; Write a function sumints that takes a number list and returns the sum of the integers:

Solution fun sumints [] = 0 sumints ((I x)::rest) = x + sumints rest sumints ((R x)::rest) = sumints rest; sumints : number list -> int;

Recursive Types Example datatype llist = Nil Node of int * llist; (* constructing instances of the linked list *) val x = Nil; val y = Node(5,Nil); val z = Node(3, Node(2, Node(1,Nil)));

Polymorphic datatype 'a llist = Nil Node of 'a * ('a llist);

MUTUAL RECURSION

Mutual recursion Example fun even 0 = true even x = odd (x - 1) and odd 0 = false odd x = even (x - 1); even : int -> bool; odd : int -> bool Note: and vs. andalso

EXCEPTIONS

Exceptions Definition and Use exception factneg of int; fun robustfact n= let fun fact 0 = 1 fact n = n * fact (n-1); in if n < 0 then raise factneg n else fact n end;

THANK YOU FOR YOUR ATTENTION See you next week