Recursion Higher Order Functions CSCE 314 Spring 2016.

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

Higher-order functions in ML
Programming with Lists
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776(Prasad)L7fold1 Fold Operations Abstracting Repetitions.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
© M. Winter COSC 4P41 – Functional Programming Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f.
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.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
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.
Functional Programming Lecture 4 - More Lists Muffy Calder.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
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,
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.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
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
Recursion.
Types CSCE 314 Spring 2016.
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Higher Order Functions
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Recursion Higher Order Functions CSCE 314 Spring 2016

CSCE 314 – Programming Studio More on Recursion: Multiple Arguments Functions can be defined with multiple arguments We’ve seen this already insert :: Ord a => a -> [a] -> [a] insert x [] = [x] insert x (y:ys) | x <= y = x:y:ys | otherwise = y:insert x ys

Spring 2016 CSCE 314 – Programming Studio More on Recursion: Multiple Recursion Functions can recursively call themselves multiple times Again, we’ve seen this before quicksort :: [a] -> [a] quicksort[] = [] quicksort (x:xs) = quicksort (smallerlist x xs) ++ [x] ++ quicksort (biggerlist x xs)

Spring 2016 CSCE 314 – Programming Studio More on Recursion: Mutual recursion Two separate functions can each call each other even 0 = True even n = odd n-1 odd 0 = False odd (n+1) = even n -- Note: this (n+1) is an integer pattern -- We can only use addition in an integer pattern

Spring 2016 CSCE 314 – Programming Studio Recursion Overview 1.Define the type 2.Enumerate the cases 3.Define the simple (base) cases 4.Define the other cases 5.Generalize and simplify

Spring 2016 CSCE 314 – Programming Studio Example: product (product of list of ints) 1.Define the type product :: [Int] -> [Int] 2. Enumerate the cases product [] product (x:xs) 3.Define the simple (base) cases product [] = 1 4.Define the other cases product (x:xs) = x * (product xs) 5.Generalize and simplify … -- can simplify with functions to be seen

Spring 2016 CSCE 314 – Programming Studio Higher-order functions We’ve seen (via currying) that a function can produce a function as output Functions can also take a function as input The map function is an example we’ve seen map :: (a -> b) -> [a] -> [b] Can define map using a list comprehension map f xs = [f x | x <- xs] Can define map using recursion map f [] = [] map f (x:xs) = f x : map f xs

Spring 2016 CSCE 314 – Programming Studio The Filter function Another higher-order function Takes in a function and a list, produces a list Function returns Bool, filter returns a list of those items that meet function filter :: (a -> Bool) -> [a] -> [Bool] Can be defined using a list comprehension filter p xs = [x <- xs, p x] Alternatively can be defined using a recursion filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs

Spring 2016 CSCE 314 – Programming Studio Some other higher-order functions all True if and only if all elements of list meet some function any True if and only if some element of a list meets some function takeWhile Takes elements of a list while they meet some function dropWhile Drops elements of a list while they meet some function

Spring 2016 CSCE 314 – Programming Studio Can you see a pattern? sum [] = 0 sum (x:xs) = x + (sum xs) product [] = 1 product (x:xs) = x * (product xs) and [] = True and (x:xs) = x && (and xs) f [] = v f (x:xs) = x op f xs v = 0 op = + v = 1 op = * v = True op = &&

Spring 2016 CSCE 314 – Programming Studio The foldr function Captures this behavior foldr :: (a -> b -> b) -> b -> [a] -> b foldr op v [] = v foldr op v (x:xs) = op x (foldr op v xs) So, the earlier examples: product ls = foldr (*) 1 ls sum ls = foldr (+) 0 ls length ls = foldr (\_ n -> 1 + n) 0 ls

Spring 2016 CSCE 314 – Programming Studio Another way of thinking of foldr Each : in the list is replaced by the binary op [] is replaced by the given value v Example: sum [1, 2, 3] = foldr (+) 0 [1, 2, 3] = foldr (+) 0 (1:(2:(3:[]))) = 1+(2(+(3+0))) = 6

Spring 2016 CSCE 314 – Programming Studio Other foldr examples The foldr function can actually represent a very wide set of functions Example: length length :: [a] -> Int length [] = 0 length (_:xs) = 1 + length xs

Spring 2016 CSCE 314 – Programming Studio foldr example for length Example: length [1, 2, 3] = length (1:(2:(3:[]))) = 1+(1+(1+0))) = 3 What is v? 0 What is op? \_ n -> 1+n So, length = foldr (\_ n -> 1+n) 0

Spring 2016 CSCE 314 – Programming Studio Why use foldr? Some recursive functions are simpler to define with foldr Properties of functions defined using foldr can be proved using algebraic properties of foldr Advanced program optimizations are simpler if foldr is used in place of explicit recursion

Spring 2016 CSCE 314 – Programming Studio Next time More on higher-order functions foldl, composition Data types