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

Similar documents
CSE 341 Section 5. Winter 2018

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

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

CSci 4223 Principles of Programming Languages

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

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

CSE 341: Programming Languages

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

List Functions, and Higher-Order Functions

Inductive Data Types

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSCI-GA Scripting Languages

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

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

Programovací jazyky F# a OCaml. Chapter 5. Hiding recursion using function-as-values

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

CSE 341 : Programming Languages Midterm, Spring 2015

Standard ML. Data types. ML Datatypes.1

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

Introduction to OCaml

FUNCTIONAL PROGRAMMING

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

The Haskell HOP: Higher-order Programming

Standard ML. Curried Functions. ML Curried Functions.1

Chapter 3 Linear Structures: Lists

A Brief Introduction to Standard ML

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

Chapter 3 Linear Structures: Lists

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Currying fun f x y = expression;

SNU Programming Language Theory

Higher-Order Functions

Part I: Written Problems

List Processing in SML

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

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CS 321 Programming Languages

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

CSE3322 Programming Languages and Implementation

Functional Paradigm II

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

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

CSE 341 : Programming Languages

Introduction to OCaml

Datatype declarations

Haskell Refresher Informatics 2D

Exercises on ML. Programming Languages. Chanseok Oh

CSE341: Programming Languages Winter 2018 Unit 3 Summary Zach Tatlock, University of Washington

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

CSCI-GA Final Exam

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

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

Continuation Passing Style. Continuation Passing Style

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

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

An introduction introduction to functional functional programming programming using usin Haskell

Handout 2 August 25, 2008

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

List Processing in SML

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

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

Programming Languages

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

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CSE 341: Programming Languages

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

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

INTRODUCTION TO HASKELL

ML Built-in Functions

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

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

"Object Oriented" Programming using ML

Lists. Michael P. Fourman. February 2, 2010

Some Advanced ML Features

L3 Programming September 19, OCaml Cheatsheet

CSE341, Spring 2013, Final Examination June 13, 2013

Higher Order Functions in Haskell

A quick introduction to SML

CSE 130, Winter 2011: Final Examination

Pattern Matching and Abstract Data Types

Chapter 6 Abstract Datatypes

CSE3322 Programming Languages and Implementation

Programming Languages and Compilers (CS 421)

Functional Paradigm II

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Introduction to Objective Caml

Functional Programming. Introduction To Cool

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Programming language seminar The F# language. Peter Sestoft Wednesday Course plan

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

Transcription:

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 Basis 2. Polymorphic Datatypes 3. First-Class Functions Anonymous Style Points Higher-Order

Standard Basis Documentation Online Documentation http://www.standardml.org/basis/index.html http://www.smlnj.org/doc/smlnj-lib/manual/toc.html Helpful Subset Top-Level http://www.standardml.org/basis/top-level-chapter.html List http://www.standardml.org/basis/list.html ListPair http://www.standardml.org/basis/list-pair.html Real http://www.standardml.org/basis/real.html String http://www.standardml.org/basis/string.html

Is Json an equality type?

Oh Shoot. How to compare? val x = String abcd ; (* type json *) val String y = x; (* now y is equality type String *) val test1 = y = abcd ;

One more note Real is not an equality type, you cannot compare them using =. Instead, you should. val x = 3.14; (* real type *) val epsilon = 0.00001; val test = x - 3.14 < epsilon;

Polymorphic Datatypes Suppose we want to create a Pair datatype A pair has two elements Both element must be of the same type datatype a pair = Pair of a * a

Now it s your term Suppose we want to create a tree datatype A node can be a leaf A node can be the root of a subtree Both leaf and non-leaf node contain some value, their value could be different E.g. Node 10 Node ( abc, Node 10, Node 20)

Now it s your term We solve this problem by having polymorphic datatypes: datatype ( a, b) tree = Leaf of a Node of b * ( a, b) tree * ( a, b) tree

Anonymous Functions An Anonymous Function fn pattern => expression An expression that creates a new function with no name. Usually used as an argument to a higher-order function. Almost equivalent to the following: let fun name pattern = expression in name end What s the difference? What can you do with one that you can t do with the other? The difference is that anonymous functions cannot be recursive!!!

Anonymous Functions What's the difference between the following two bindings? val name = fn pattern => expression; fun name pattern = expression; Once again, the difference is recursion. However, excluding recursion, a fun binding could just be syntactic sugar for a val binding and an anonymous function.

Something is wrong. What s wrong with these expressions? (if ex then true else false) (fn xs => tl xs)

Unnecessary Function Wrapping What's the difference between the following two expressions? (fn xs => tl xs) vs. tl STYLE POINTS! Other than style, these two expressions result in the exact same thing. However, one creates an unnecessary function to wrap tl. This is very similar to this style issue: (if ex then true else false) vs. ex

Higher-Order Functions Definition: A function that returns a function or takes a function as an argument. SML functions can be passed around like any other value. They can be passed as function arguments, returned, and even stored in data structures or variables. Generalized functions such as these are very pervasive in functional languages (and are starting to creep into more Object-Oriented ones too, e.g. Java)

Note: List.map, List.filter, and List.foldr/foldl are similarly defined in SML but use currying. We'll cover these later in the course. Canonical Higher-Order Functions

map map : ('a -> 'b) * 'a list -> 'b list What does the type tell is? What are the arguments? What is the return type?

map map : ('a -> 'b) * 'a list -> 'b list What does the type tell is? What are the arguments? What is the return type? map applies a function to every element of a list and return a list of the resulting values. Example: map (fn x => x*3, [1,2,3]) === [3,6,9]

map Sample: map (fn x => x*3, [1,2,3]) [1, 2, 3]

map Sample: map (fn x => x*3, [1,2,3]) [1, 2, 3] [,, ]

map Sample: map (fn x => x*3, [1,2,3]) [1, 2, 3] fn 1 => 1*3 [3,, ]

map Sample: map (fn x => x*3, [1,2,3]) [1, 2, 3] fn 1 => 1*3 fn 2 => 2*3 [3, 6, ]

map Sample: map (fn x => x*3, [1,2,3]) [1, 2, 3] fn 1 => 1*3 fn 2 => 2*3 fn 3 => 3*3 [3, 6, 9]

flat_map flat_map : ('a -> 'b list) * 'a list -> 'b list map : ('a -> 'b) * 'a list -> 'b list Notice the difference?

flat_map flat_map : ('a -> 'b list) * 'a list -> 'b list map : ('a -> 'b) * 'a list -> 'b list Notice the difference? flat_map applies a function which returns a list to every element of a list and return a concatenated list of the resulting lists. Example: flat_map (fn x => [x,~x], [1,2,3]) === [1,~1,2,~2,3,~3]

flat_map Sample: flat_map (fn x => [x,~x], [1,2,3]) [1, 2, 3]

flat_map Sample: flat_map (fn x => [x,~x], [1,2,3]) [1, 2, 3] [,, ],,,

flat_map Sample: flat_map (fn x => [x,~x], [1,2,3]) [1, 2, 3] fn 1 => [1,~1] [1,~1,,,, ]

flat_map Sample: flat_map (fn x => [x,~x], [1,2,3]) [1, 2, 3] fn 1 => [1,~1] fn 2 => [2,~2] [1,~1,2,~2,, ]

flat_map Sample: flat_map (fn x => [x,~x], [1,2,3]) [1, 2, 3] fn 1 => [1,~1] fn 2 => [2,~2] fn 3 => [3,~3] [1,~1,2,~2,3,~3]

filter filter : ('a -> bool) * 'a list -> 'a list What could be the type of this function? What are the arguments? What is the return type?

filter filter : ('a -> bool) * 'a list -> 'a list What could be the type of this function? What are the arguments? What is the return type? filter returns the list of elements from the original list that, when a predicate function is applied, result in true. Example: filter (fn x => x>2, [~5,3,2,5]) === [3,5]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3] [????]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3] fn 1 => 1 > 1 [???]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3] fn 1 => 1 > 1 fn 2 => 2 > 1 [ 2,??]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3] fn 1 => 1 > 1 fn 2 => 2 > 1 fn 0 => 0 > 1 [ 2,?]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3] fn 1 => 1 > 1 fn 2 => 2 > 1 fn 0 => 0 > 1 fn 3 => 3 > 1 [ 2, 3]

filter Sample: filter (fn x => x > 1, [1,2,0,3]) [1, 2, 0, 3] fn 2 => 2 > 1 fn 3 => 3 > 1 [2, 3]

fold fold : ('a * 'b -> 'a) * 'a * 'b list -> 'a Returns a thing that is the accumulation of the first argument applied to the third arguments elements stored in the second argument. Example: fold((fn (a,b) => a + b), 0, [1,2,3]) === 6

fold acc = Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) [2, 1, 4] 1

fold Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) 1 [2, 1, 4] acc = fn (1, 2) => 1*2

fold Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) 1 acc = [2, 1, 4] fn (1, 2) => 1*2 2

fold Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) 1 acc = [2, 1, 4] fn (1, 2) => 1*2 2 fn (2, 1) => 2*1

fold Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) 1 [2, 1, 4] fn (1, 2) => 1*2 2 fn (2, 1) => 2*1 acc = 2

fold Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) 1 [2, 1, 4] fn (1, 2) => 1*2 2 fn (2, 1) => 2*1 acc = 2 fn (2, 4) => 2*4

fold Sample: fold (fn (acc, x) => acc * x, 1, [2, 1, 4]) 1 [2, 1, 4] fn (1, 2) => 1*2 2 fn (2, 1) => 2*1 2 fn (2, 4) => 2*4 acc = 8