Functional Programming Lecture 12 - more higher order functions

Slides:



Advertisements
Similar presentations
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.
Advertisements

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Chapter 9 More About Higher-Order Functions. Currying Recall the function: simple n a b = n * (a+b) Note that: simple n a b is really (((simple n) a)
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Chapter 5 Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
Tuples and Lists Lecture 3, Programmeringsteknik del A.
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: More on Functions and List Comprehensions 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.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
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.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Lecture 14: Advanced Topic: Functional Programming
Polymorphic Functions
Functional Programming
Conditional Expressions
Recursion.
PROGRAMMING IN HASKELL
dr Robert Kowalczyk WMiI UŁ
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Chapter 1, Part II.
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
Functional Programming
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Lecture 15 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
Abstracting Repetitions
PROGRAMMING IN HASKELL
Advanced Functional Programming
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Madhusudan Parthasarathy
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
Functional Programming
Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Programming Languages Dan Grossman 2013
Presentation transcript:

Functional Programming Lecture 12 - more higher order functions

Recursion over two arguments The function zip “zips” together two lists, to produce a new list of pairs. zip :: [a] -> [b] -> [(a,b)] zip (x:xs) (y:ys) = (x,y) : zip xs ys zip _ _ = [] or zip [] (y:ys) = [] zip xs [] = [] (Why not zip (x:xs) [] = [] ?) E.g. zip [1,2,3] [‘a’,’b’,’c’] => [(1,’a’), (2,’b’), (3,’c’)] zip [1,2,3] [‘a’,’b’] => [(1,’a’), (2,’b’)]

The function unzip “unzips” a list of pairs to produce a pair of lists. unzip :: [(a,b)] -> ([a],[b]) tricky definition unzip [] = ([],[]) unzip (x,y):zs = (x:xs,y:ys) where (xs,ys) = unzip zs What is unzip [(1,’a’), (2,’b’), (3,’c’)]? => ([1,2,3],[‘a’,’b’,’c’]) What is unzip (zip xs ys) ? => (xs,ys) if xs and ys have same length What is zip (unzip zs)? => type error, unzip has produced a tuple! What is zip (fst (unzip zs)) (snd (unzip zs)) ? => zs

Fold and Foldr Consider computing the sum of the elements of a list [e1, e2, … en] i.e. e1 + (e2 +( … + en )). sumlist :: [Int] -> Int sumlist [x] = x sumlist (x:xs) = x + sumlist xs Now consider computing the maximum of the elements of a list [e1, e2, … en] i.e. e1 max (e2 max ( … max en)). maxlist :: [Int] -> Int maxlist [x] = x maxlist (x:xs) = max x (maxlist xs) In both cases, we are “folding” the binary function + and max through a non-empty list.

Fold Define fold as the function fold :: (a -> a -> a) -> [a] -> a a binary function a list the result fold f [x] = x fold f (x:xs) = f x (fold f xs) Note: although [x] matches the second equation, we will always apply the first equation. Examples fold (||) [False, True, False] => True fold (++) [“Hello “, “world”, “!”] => “Hello world!” fold (*) [1..5] => 120 n.b infix op. Becomes prefix in ‘(‘ ‘)’

But fold has not been defined for an empty list. What would we like to define fold (||) [] and fold (*) [] as ? There is no general answer, we must provide one for each function we give to fold. The function which defines fold for an empty list is foldr.

Foldr Define foldr as the function foldr :: (a -> b -> b) -> b -> [a] -> b a binary function empty value a list the result Note: foldr has a more general type. foldr f st [] = st foldr f st (x:xs) = f x (foldr f st xs) Note: foldr means fold right. Usually the extra argument is the identity element, i.e. the stopping value st is the identity for f. foldr is defined in the standard prelude.

Examples foldr (||) False [False, True, False] => True foldr (&&) True [True, True] foldr (&&) False [True, True] => False foldr (*) 1 [1..5] => 120 1 is an identity for * foldr (*) 0 [1..5] => 0 because 0 is a zero for * fac n = foldr (*) 1 [1..n]