GC16/3011 Functional Programming Lecture 9 Higher Order Functions

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Com Functional Programming Higher Order Functions and Computation Patterns (II) Marian Gheorghe Lecture 11 Module homepage Mole &
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Polymorphic Functions
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
CSE341: Programming Languages Lecture 11 Type Inference
Recursion.
Functions.
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
Haskell Chapter 1, Part II.
Functions and patterns
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
CMSC 330: Organization of Programming Languages
Important Concepts from Clojure
PROGRAMMING IN HASKELL
Important Concepts from Clojure
Higher-Order Procedures
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Higher Order Functions
CSE341: Programming Languages Lecture 11 Type Inference
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 11 Type Inference
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Important Concepts from Clojure
Functions and patterns
CSE341: Programming Languages Lecture 11 Type Inference
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 11 Type Inference
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

GC16/3011 Functional Programming Lecture 9 Higher Order Functions

Contents Higher Order Functions Combinators Definition Example: composition Combinators Example: S, K, I Capturing common forms of recursion on lists Examples: map and filter

Higher Order Functions Definition: A Higher Order Function is a function that either (i) takes a function as an argument, or (ii) returns a function as a result, or (iii) both. f g x = (g x) h x = (+ x) j f p g = (+ (f p)), if p = (+ (g p)), otherwise

Higher Order Functions: types f g x = (g x) h :: num -> (num -> num) h x = (+ x) j :: (bool-> num) -> bool -> (bool -> num) -> (num -> num) j f p g = (+ (f p)), if p = (+ (g p)), otherwise

Function composition Can partially apply “compose”: compose f g x = f (g x) Can partially apply “compose”: fred = (compose (+ 1) abs) main = fred (-3) Built-in operator “.” fred = ((+1) . abs)

Function composition (2) twice x = x * 2 many x = twice (twice (twice (twice x))) mymany :: num -> num mymany = (twice . twice . twice . twice)

Combinators Definition Examples: A combinator is a function that contains no free variables Examples: fst (x,y) = x id x = x || “I” cancel x y = x || “K” swap f x y = f y x || “C” distribute f g x = f x (g x) || “S”

Combinators (2) Basis for implementation S & K computationally complete All required data available via arguments (passed on the stack) so no need to search for distant bindings

Example HOF “myiterate” repeatedly applies its second parameter to its final parameter; the final parameter is an accumulator for the result. The function loops n times, where n is the first parameter: myiterate :: num -> (* -> *) -> * -> * myiterate 0 f state = state myiterate n f state = myiterate (n-1) f (f state)

Use of example HOF printdots n = myiterate n ((++) “.”) “” printdots 3 ((++) “.” ((++) “.” ((++) “.” “”))) “…”

Recursion on lists: capturing common forms Mapping a function across the values of a list: notlist [] = [] notlist (x:xs) = ((~ x) : (notlist xs)) inclist [] = [] inclist (x:xs) = ((x+1) : (inclist xs)) abslist [] = [] abslist (x:xs) = ((abs x) : (abslist xs))

Recursion on lists: map Built-in function “map” makes life easier: notlist any = map (~) any inclist any = map (+1) any abslist any = map abs any Or, simply: notlist = map (~) inclist = map (+1) abslist = map abs

Definition of map map f [] = [] map f (x:xs) = ((f x): (map f xs)) So, what’s the type of map? Write it here:

Recursion on lists: capturing common forms Filtering some elements out of a list: firsts [] = [] firsts (x:xs) = (x : (firsts xs)), if (x>=70) = firsts xs, otherwise not34 [] = [] not34 (x:xs) = (x : (not34 xs)), if (x~=34) = not34 xs, otherwise

Recursion on lists: filter Built-in function “filter” makes life easier: firsts any = filter (>=70) any not34 any = filter (~=34) any Or, simply: firsts = filter (>=70) not34 = filter (~=34)

Definition of filter filter f [] = [] filter f (x:xs) = (x: (filter f xs)), if (f x) = filter f xs, otherwise So, what’s the type of filter? Write it here:

Challenge Can you write a function that takes a list of numbers (containing only the values 1, 2 and 0, where at least one 0 must occur) and returns a three-tuple containing: The number of 1s before the first 0 The number of 2s before the first 0 The length of the longest sequence of 1s before the first 0 If you find that easy, try designing the program a different way so that it makes use of higher order functions (e.g. filter, dropwhile, takewhile)

Summary Higher Order Functions Combinators Use of example HOF Definition Example: composition Combinators Example: S, K, I Use of example HOF Capturing common forms of recursion on lists Examples: map and filter