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

ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
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.
Haskell Chapter 5, Part I. Topics  Higher Order Functions  map, filter  Infinite lists Get out a piece of paper… we’ll be doing lots of tracing.
© 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.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
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.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Functional Programming in Scheme and Lisp.
Haskell Chapter 5, Part III. Recap  Lambda functions  numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100]))  What’s wrong with.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
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.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
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.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
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.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© 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.
Polymorphic Functions
Functional Programming
Recursion.
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
dr Robert Kowalczyk WMiI UŁ
Haskell Chapter 5, Part III.
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
Haskell.
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CMSC 330: Organization of Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Clojure to Haskell (It’s mostly syntax).
Abstracting Repetitions
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
Fundamentals of Functional Programming
Introduction to Functional Programming in Racket
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
Functions and patterns
PROGRAMMING IN HASKELL
Functional Programming
Functions and patterns
Higher-Order Functions in Haskell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
Presentation transcript:

Higher Order Functions Haskell Higher Order Functions

Introduction Haskell functions can take functions as parameters and return functions as return values. A function that does either of those is called a higher order function.

Curried functions ghci> max 4 5 5 All the functions that accept several parameters are curried functions. ghci> max 4 5 5 ghci> (max 4) 5 max :: (Ord a) => a -> a -> a max :: (Ord a) => a -> (a -> a)

Partially Applied Function multThree :: (Num a) => a -> a -> a -> a multThree :: (Num a) => a -> (a -> (a -> a)) multThree x y z = x * y * z

ghci> let multTwoWithNine = multThree 9 ghci> multTwoWithNine 2 3 54

compareWithHundred x = compare 100 x compareWithHundred :: (Num a, Ord a) => a -> Ordering compareWithHundred x = compare 100 x compareWithHundred :: (Num a, Ord a) => a -> Ordering compareWithHundred = compare 100

Infix Functions divideByTen :: (Floating a) => a -> a

Higher-Orderism Functions can take functions as parameters and also return functions. applyTwice :: (a -> a) -> a -> a applyTwice f x = f (f x)

zipWith zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys

ghci> zipWith' (+) [4,2,5,6] [2,6,2,3] [6,8,7,9] ghci> zipWith' max [6,3,2,1] [7,3,1,5] [7,3,2,5]

Maps and Filters map takes a function and a list and applies that function to every element in the list, producing a new list. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs

Examples of using map ghci> map (+3) [1,5,3,1,6] [4,8,6,4,9] ghci> map (++ "!") ["BIFF", "BANG", "POW"] ["BIFF!","BANG!","POW!"] ghci> map (replicate 3) [3..6] [[3,3,3],[4,4,4],[5,5,5],[6,6,6]] ghci> map (map (^2)) [[1,2],[3,4,5,6],[7,8]] [[1,4],[9,16,25,36],[49,64]]

Using Filter filter is a function that takes a predicate (a predicate is a function that tells whether something is true or not, so in our case, a function that returns a boolean value) and a list and then returns the list of elements that satisfy the predicate. filter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs

Examples ghci> filter (>3) [1,5,3,2,1,6,4,3,2,1] [5,6,4] [3] ghci> filter even [1..10] [2,4,6,8,10]

more examples of using filter largestDivisible :: (Integral a) => a largestDivisible = head (filter p [100000,99999..]) where p x = x `mod` 3829 == 0

Lambdas Lambdas are basically anonymous functions that are used because we need some functions only once. Normally, we make a lambda with the sole purpose of passing it to a higher-order function.

Examples chain :: (Integral a) => a -> [a] chain 1 = [1] chain n | even n = n:chain (n `div` 2) | odd n = n:chain (n*3 + 1) numLongChains :: Int numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100]))

Folds and Horses A fold takes a binary function, a starting value (accumulator) and a list to fold up. The binary function itself takes two parameters. The binary function is called with the accumulator and the first (or last) element and produces a new accumulator. Then, the binary function is called again with the new accumulator and the now new first (or last) element, and so on. Once we've walked over the whole list, only the accumulator remains, which is what we've reduced the list to.

sum' :: (Num a) => [a] -> a Left Fold sum' :: (Num a) => [a] -> a sum' xs = foldl (\acc x -> acc + x) 0 xs ghci> sum' [3,5,2,1] 11

map' :: (a -> b) -> [a] -> [b] Right Fold map' :: (a -> b) -> [a] -> [b] map' f xs = foldr (\x acc -> f x : acc) [] xs map' f xs = foldl (\acc x -> acc ++ [f x]) [] xs

sum (map sqrt [1..130]) = sum $ map sqrt [1..130] Function application with $ Function application with a space is left-associative function application with $ is right-associative. example: sum (map sqrt [1..130]) = sum $ map sqrt [1..130] Function application can be treated just like another function. ghci> map ($ 3) [(4+), (10*), (^2), sqrt] [7.0,30.0,9.0,1.7320508075688772]

Function composition ghci> map (\x -> negate (abs x)) [5,-3,-6,7,-3,2,-19,24] [-5,-3,-6,-7,-3,-2,-19,-24] ghci> map (negate . abs) [5,-3,-6,7,-3,2,-19,24] Function composition is right-associative, so we can compose many functions at a time. f (g (z x)) is equivalent to (f . g . z) x. ghci> map (\xs -> negate (sum (tail xs))) [[1..5],[3..6],[1..7]] [-14,-15,-27] ghci> map (negate . sum . tail) [[1..5],[3..6],[1..7]]

Haskell VS C++ http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=gpp&lang2=ghc