Higher-Order Functions

Similar documents
Lecture 2: The Basis of SML

CSci 4223 Principles of Programming Languages

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Inductive Data Types

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Chapter 3 Linear Structures: Lists

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

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

Chapter 3 Linear Structures: Lists

Lecture 19: Functions, Types and Data Structures in Haskell

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

Exercises on ML. Programming Languages. Chanseok Oh

A general introduction to Functional Programming using Haskell

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

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

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

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

ML Built-in Functions

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

SML A F unctional Functional Language Language Lecture 19

CSE 341 Section 5. Winter 2018

CSCE 314 Programming Languages

Chapter 6 Abstract Datatypes

Shell CSCE 314 TAMU. Higher Order Functions

CSE 130 [Winter 2014] Programming Languages

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

Lists. Michael P. Fourman. February 2, 2010

CS 340 Spring 2019 Midterm Exam

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

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

A list is a finite sequence of elements. Elements may appear more than once

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

Standard ML. Data types. ML Datatypes.1

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

Logic - CM0845 Introduction to Haskell

Fall Lecture 3 September 4. Stephen Brookes

Lecture 4: Higher Order Functions

Handout 2 August 25, 2008

The Haskell HOP: Higher-order Programming

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

F28PL1 Programming Languages. Lecture 12: Standard ML 2

A Brief Introduction to Standard ML

CSE 341 Sample Midterm #2

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

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

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

Problem Set CVO 103, Spring 2018

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Functional Programming - 2. Higher Order Functions

CSE341 Spring 2016, Midterm Examination April 29, 2016

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CSE3322 Programming Languages and Implementation

Introduction to Programming: Lecture 6

CSE3322 Programming Languages and Implementation

A quick introduction to SML

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

Lecture Notes on Induction and Recursion

CS115 - Module 8 - Binary trees

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

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Module 9: Binary trees

Functional Paradigm II

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

02157 Functional Programming Tagged values and Higher-order list functions

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

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

List Processing in SML

Functional Programming in Haskell Part I : Basics

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4]

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

CSC324 Functional Programming Typing, Exceptions in ML

Higher Order Functions in Haskell

02157 Functional Programming Lecture 1: Introduction and Getting Started

CSE341 Spring 2016, Midterm Examination April 29, 2016

Module 8: Binary trees

List Processing in SML

CS 440: Programming Languages and Translators, Spring 2019 Mon

Standard ML. Curried Functions. ML Curried Functions.1

Functional Programming and Haskell

Programming Language Concepts, CS2104 Lecture 7

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

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

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)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

Introduction to OCaml

Fall 2018 Lecture N

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

CS 209 Functional Programming

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

Lecture 21: Functional Programming in Python. List Comprehensions

Transcription:

Higher-Order Functions Tjark Weber Functional Programming 1 Based on notes by Pierre Flener, Jean-Noël Monette, Sven-Olof Nyström Tjark Weber (UU) Higher-Order Functions 1 / 1

Tail Recursion http://xkcd.com/1270/ Tjark Weber (UU) Higher-Order Functions 2 / 1

Exercise: A Simple Matrix Can you find a linear tail-recursive function, a linear function that is not tail-recursive, a quadratic but tail-recursive function, and a quadratic function that is not tail-recursive? Tail-recursive Not tail-recursive Linear?? Quadratic?? Tjark Weber (UU) Higher-Order Functions 3 / 1

Higher-Order Functions Tjark Weber (UU) Higher-Order Functions 4 / 1

Today Tjark Weber (UU) Higher-Order Functions 5 / 1

Definition, Introductory Examples Table of Contents Tjark Weber (UU) Higher-Order Functions 6 / 1

Definition, Introductory Examples Higher-Order Functions A higher-order function is a function that Remarks: takes functions as arguments or returns a function. This is hard to do in most imperative programming languages. This is a very powerful mechanism. Abstraction, code re-use Tjark Weber (UU) Higher-Order Functions 7 / 1

Definition, Introductory Examples An Example From Mathematics The derivative in calculus maps (differentiable) functions to functions. Tjark Weber (UU) Higher-Order Functions 8 / 1

Definition, Introductory Examples An Example From Last Lecture fun time f = l e t v a l t i m e r = Timer. startcputimer ( ) v a l = f ( ) ( do the a c t u a l work ) i n Timer. checkcputimes t i m e r end Here, f is a function (of type unit > a). Thus, time is a higher-order function. Tjark Weber (UU) Higher-Order Functions 9 / 1

Definition, Introductory Examples Another Example: Function Composition A function that takes two functions and returns a function! fun o (f,g) x = f (g x); infix o; o is predefined in SML. No need to define it yourself. Example: fun add1 x = x + 1; (add1 o add1) 42; Tjark Weber (UU) Higher-Order Functions 10 / 1

Definition, Introductory Examples Insertion Sort on Integers fun insert x [] = [x] insert x (y :: ys) = if x < y then x :: y :: ys else y :: ( insert x ys) fun isort [] = [] isort (x :: xs) = insert x ( isort xs) Which part of the code is specific to integers? Tjark Weber (UU) Higher-Order Functions 11 / 1

Definition, Introductory Examples Insertion Sort on Any Type fun insert order x [] = [x] insert order x (y :: ys) = if order (x,y) then x :: y :: ys else y :: ( insert order x ys) fun isort order [] = [] isort order (x :: xs) = insert order x ( isort order xs) What is the type of the different functions? Tjark Weber (UU) Higher-Order Functions 12 / 1

Definition, Introductory Examples Example (cont.) > isort (op <) [6,3,0,1,7,8,5,9,2,4]; val it = [0,1,2,3,4,5,6,7,8,9]: int list > isort (op >) [6,3,0,1,7,8,5,9,2,4]; val it = [9,8,7,6,5,4,3,2,1,0]: int list > isort String.< [ one, two, three, four, five, six ]; val it = [ five, four, one, six, three, two ]: string > isort (op <); val it = fn: int list > int list > isort String.< ; val it = fn: string list > string list > isort (fn ((,s1 ),(,s2)) => String.< (s1,s2)) [(1, one ),(2, two ),(3, three ),(4, four ),(5, five ),(6, six )]; val it = [(5, five ),(4, four ),(1, one ),(6, six ),(3, three ), (2, two )]: ( int string ) list list Tjark Weber (UU) Higher-Order Functions 13 / 1

Definition, Introductory Examples Another Example: pair > fun pair (f,g) x = (f x, g x); val pair = fn: ( a > b) ( a > c) > a > b c > pair (add1, add1 o add1); val it = fn: int > int int > it 42; val it = (43,44): int int > pair ( pair (add1, add1 o add1), add1); val it = fn: int > (int int) int > it 42; val it = ((43,44),43): ( int int ) int Tjark Weber (UU) Higher-Order Functions 14 / 1

Higher-Order Functions on Lists Table of Contents Tjark Weber (UU) Higher-Order Functions 15 / 1

Higher-Order Functions on Lists Reflection on the Definition of sum fun sum [] = 0 sum (x::xs) = x + sum xs There are only two places in the function definition that are specific for computing a sum: +, combining data 0, result for empty list Tjark Weber (UU) Higher-Order Functions 16 / 1

Higher-Order Functions on Lists A Generalization Let s define a function reduce so that reduce + 0 is equal to sum: fun reduce f z [] = z reduce f z (x :: xs) = f (x,reduce f z xs) fun reduce f z = (fn [] => z (x :: xs) => f (x,reduce f z xs)) Note the similarity between reduce and sum. Tjark Weber (UU) Higher-Order Functions 17 / 1

Higher-Order Functions on Lists Generalization (cont.) Now, sum can be defined as fun sum xs = reduce (op +) 0 xs val sum = reduce (op +) 0 Tjark Weber (UU) Higher-Order Functions 18 / 1

Higher-Order Functions on Lists Other Uses of reduce What will the following compute? reduce (op ) 1 [1,2,3,4]; reduce Int. max 0 [1,2,3,44,5,6]; reduce (fn(x,y) => x::y) [] [1,2,3,4]; reduce (fn(x,y) => x::y) [5,6,7] [1,2,3,4]; reduce (fn(x,y) => x::x::y) [] [1,2,3,4]; reduce (op @) [] [[1,2],[34],[5,6,7,89]]; Tjark Weber (UU) Higher-Order Functions 19 / 1

Higher-Order Functions on Lists Higher-Order Functions on Lists Several functions are very useful to define operations on lists. > map; val it = fn: ( a > b) > a list > b list > foldr ; val it = fn: ( a b > b) > b > a list > b > foldl ; val it = fn: ( a b > b) > b > a list > b > List. filter ; val it = fn: ( a > bool) > a list > a list Those functions are predefined in ML, but we will see the details. Tjark Weber (UU) Higher-Order Functions 20 / 1

Higher-Order Functions on Lists The map Function Apply the same operation on all the elements of a list. ( PRE: (none) POST: [f(a 1),f(a 2),..., f(a n )], if L = [a 1,a 2,..., a n] ) > fun map f [ ] = [ ] map f (x :: xs) = f x :: map f xs; val map = fn: ( a > b) > a list > b list > fun square x = x x; val square = fn: int > int > map square [1,2,3,4]; val it = [1,4,9,16]: int list > map (fn(x)=> if x < 3 then 0 else x) [1,2,3,4]; val it = [0,0,3,4]: int list Tjark Weber (UU) Higher-Order Functions 21 / 1

Higher-Order Functions on Lists The map Function (cont.) > map square; val it = fn: int list > int list > map (map square); val it = fn: int list list > int list list > map (map square) [[1,2,34],[5]]; val it = [[1,4,1156],[25]]: int list list > map String.<; val it = fn: ( string string ) list > bool list > map String.< [( a, ab ), ( hello, bye )]; val it = [true, false ]: bool list Tjark Weber (UU) Higher-Order Functions 22 / 1

Higher-Order Functions on Lists Foldr and foldl fun foldr f b [] = b foldr f b (x :: xs) = f(x, foldr f b xs ); fun foldl f b [] = b foldl f b (x :: xs) = foldl f (f (x, b)) xs; Do they remind you of any function you have seen before? Which one is tail-recursive? Tjark Weber (UU) Higher-Order Functions 23 / 1

Higher-Order Functions on Lists Examples Sum the elements of the list. foldr (op +) 0 [0,2,21,4,6]; foldl (op +) 0 [0,2,21,4,6]; Are they equivalent? Which one would you use? Why? Tjark Weber (UU) Higher-Order Functions 24 / 1

Higher-Order Functions on Lists Examples (cont.) Check that all elements of a list are even foldr (fn (x,y) => y andalso x mod 2 = 0) true [0,2,21,4,6]; foldl (fn (x,y) => y andalso x mod 2 = 0) true [0,2,21,4,6]; Are they equivalent? Which one would you use? Why? Tjark Weber (UU) Higher-Order Functions 25 / 1

Higher-Order Functions on Lists Examples (cont.) Compare foldr (op ::) []; foldl (op ::) []; Are they equivalent? Which one would you use? Why? Tjark Weber (UU) Higher-Order Functions 26 / 1

Higher-Order Functions on Lists Examples (cont.) What does this function compute? fun firsteven (x,none) = if x mod 2 = 0 then SOME x else NONE firsteven (, SOME y) = SOME y; Compare foldl foldr firsteven NONE; firsteven NONE; Are they equivalent? Which one would you use for what? Tjark Weber (UU) Higher-Order Functions 27 / 1

Higher-Order Functions on Lists More Examples Try foldl (fn(x,n) => n+1) 0 foldr (fn(x,n) => n+1) 0 foldr (fn(x,n) => x) 0 foldl (fn(x,n) => x) 0 fun mystery f = foldr (fn (x,n) => f x :: n) []; Tjark Weber (UU) Higher-Order Functions 28 / 1

Higher-Order Functions on Lists Filter ( PRE: (none) POST: the list of elements of the list for which p is true ) fun filter p [] = [] filter p (x :: xs) = if p x then x :: filter p xs else filter p xs; Examples: filter (fn x => x<6); filter (fn x => x<6) [6,3,0,1,8,5,9,3]; Tjark Weber (UU) Higher-Order Functions 29 / 1

More Examples Table of Contents Tjark Weber (UU) Higher-Order Functions 30 / 1

More Examples Folding Over Integers Fold over integers (really natural numbers) gives us a general way to recurse over natural numbers: fun foldint f b 0 = b foldint f b n = foldint f (f(b,n)) (n 1); foldint (op + ) 0 5; foldint (op ) 1 5; foldint (fn ((a,b), ) => (a+b, a)) (1,1) 10; Tjark Weber (UU) Higher-Order Functions 31 / 1

More Examples Folding Over Integers (cont.) fun td a = foldint (fn (b,n) => b andalso (n <= 1 orelse a mod n <> 0)) true (a 1); fun primes n = foldint (fn ( l, n) => if td n then n:: l else l ) [] n; Tjark Weber (UU) Higher-Order Functions 32 / 1

More Examples twice and ntimes We can define a function that tells us how to do something twice: fun twice f = f o f ; fun add1 x = x+1; twice add1 56; Or more generally, do something n times: fun ntimes 0 f x = x ntimes n f x = ntimes (n 1) f (f x); ntimes 42 twice ; Tjark Weber (UU) Higher-Order Functions 33 / 1

Example: Polymorphic Ordered Binary Tree Table of Contents Tjark Weber (UU) Higher-Order Functions 34 / 1

Example: Polymorphic Ordered Binary Tree Binary Search Trees A Binary search tree is a binary tree which is ordered, i.e. All values in the left subtree are smaller than the value of the root. All values in the right subtree are larger than the value of the root. The order can be defined for any type. We will consider the values to be pairs: The first element is the key. The second is the value that we want to associate with the key. i.e. we define a dictionary. Tjark Weber (UU) Higher-Order Functions 35 / 1

Example: Polymorphic Ordered Binary Tree Binary Search Trees datatype ( a, b) bstree = Void Bst of ( a, b) bstree ( a b) ( a, b) bstree; Note: We want the key to be of some equality type. The order is not part of the datatype. It is (unfortunately) possible to build a bstree that is not ordered. Tjark Weber (UU) Higher-Order Functions 36 / 1

Example: Polymorphic Ordered Binary Tree Retrieving an Element ( PRE: the tree is ordered. POST: if the tree contains the key, return the corresponding value embedded in SOME. NONE otherwise ) fun retrieve lessthan k Void = NONE retrieve lessthan key (Bst ( left,( k,v), right )) = if key = k then SOME v else if lessthan (key,k) then retrieve lessthan key left else retrieve lessthan key right ; Tjark Weber (UU) Higher-Order Functions 37 / 1

Example: Polymorphic Ordered Binary Tree Retrieving an Element (cont.) We may use the datatype order instead: fun retrieve compare k Void = NONE retrieve compare key (Bst ( left,( k,v), right )) = case compare (key,k) of EQUAL => SOME v LESS => retrieve compare key left GREATER => retrieve compare key right; Tjark Weber (UU) Higher-Order Functions 38 / 1

Example: Polymorphic Ordered Binary Tree Inserting an Element ( PRE: The tree is ordered. POST: If the tree contains the key, a tree with the value replaced. Otherwise, a tree with the (key, value) pair inserted such that the tree is ordered. ) fun insert compare (key,value) Void = Bst(Void,(key,value ), Void) insert compare (key,value) (Bst ( left,( k,v), right )) = case compare (key,k) of EQUAL => Bst (left,(k,value),right) LESS => Bst(insert compare (key,value) left, (k,v), right ) GREATER => Bst(left,(k,v),insert compare (key,value) right ); Exercise: Specify and realise the exists and delete functions. (Delete is a bit more difficult.) Tjark Weber (UU) Higher-Order Functions 39 / 1

Example: Polymorphic Ordered Binary Tree Functional Datatype Consider again the previous definition of bstree and its shortcomings: The functional argument compare must be passed at each call. The compare order is not global to the binary search tree. Let s introduce the order in a new datatype: datatype ( a, b) bstree = Void Bst of ( a, b) bstree ( a b) ( a, b) bstree; type ( a) ordering = ( a a) > order; datatype ( a, b) obstree = OrdBsTree of a ordering ( a, b) bstree; Tjark Weber (UU) Higher-Order Functions 40 / 1

Example: Polymorphic Ordered Binary Tree Inserting an Element fun emptytree compare = OrdBsTree (compare, Void); fun insert (key, value) OrdBsTree (compare, tree) = let fun insert Void = (key,value) insert (Bst ( left,( k,v), right )) = case compare (key,k) of EQUAL => Bst (left,(k,value),right) LESS => Bst(insert left, (k,v), right ) GREATER => Bst(left,(k,v),insert right ) in OrdBstTree (compare, insert tree ) end; Tjark Weber (UU) Higher-Order Functions 41 / 1

Higher-Order Functions on Trees Table of Contents Tjark Weber (UU) Higher-Order Functions 42 / 1

Higher-Order Functions on Trees Higher-Order Functions on Trees Like for lists, it is possible to define generic functions on trees. mapbt applies some function on all elements of the tree. Different folding functions can be defined. We cover here binary trees, but the same might be done for other types of trees. Tjark Weber (UU) Higher-Order Functions 43 / 1

Higher-Order Functions on Trees Mapping and Reducing a Binary Tree datatype ( a) btree = Vd Bt of a a btree a btree ; fun mapbt f Vd = Vd mapbt f (Bt(v, l, r)) = Bt(f v, mapbt f l, mapbt f r ); fun reducebt f z Vd = z reducebt f z (Bt(v, l, r)) = f (v,reducebt f z l,reducebt f z r ); mapbt abs; reducebt (fn (v, l, r) => 1 + l + r) 0; reducebt (fn (v, l, r) => 1 + Int.max(l,r)) 0; reducebt (fn (v, l, r) => l @ [v] @ r) []; reducebt (fn (v, l, r) => Bt(v,r,l)) Vd; Tjark Weber (UU) Higher-Order Functions 44 / 1

Higher-Order Functions on Trees Exercises Use reducebt to compute if a btree is ordered (according to some given order). Use reducebt to transform a btree into a bstree. Define a function map on finitely branching trees (cf. assignment 2). Tjark Weber (UU) Higher-Order Functions 45 / 1