© M. Winter COSC 4P41 – Functional Programming 4.14.1 Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
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.
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.
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.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
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.
Cs536 Functional Programming 16/13/2015 Lecture #2.5 Today’s Topics A Few Less Trivial Examples – Numerical Functions »Differentiation and square root.
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 Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
Chapter 5 Polymorphic and Higher-Order Functions.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
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,
Creating Functions Functional Programming. The function calculator Functional programming is all about using functions Functions are first class – Take.
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.
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.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Lecture 2 – MapReduce: Theory and Implementation CSE 490h – Introduction to Distributed Computing, Winter 2008 Except as otherwise noted, the content of.
© M. Winter COSC 4P41 – Functional Programming Modules in Haskell Using modules to structure a large program has a number of advantages: Parts of.
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.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
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 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
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
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.
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
Recursion.
Types CSCE 314 Spring 2016.
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
Haskell.
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
Introduction to Functional Programming in Racket
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
Abstracting Repetitions
PROGRAMMING IN HASKELL
Pattern Matching Pattern matching allows us to do things like this:
Higher Order Functions
CS 457/557: Functional Languages Folds
Introduction to Functional Programming in Racket
CSE-321 Programming Languages Introduction to Functional Programming
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Functions and patterns
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
Functions and patterns
Presentation transcript:

© M. Winter COSC 4P41 – Functional Programming Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f xs = [f x | x <- xs] Selecting elements – filtering filter :: (a -> Bool) -> [a] -> [a] filter p xs = [x | x<- xs, p x] Combing the items – folding foldr :: (a -> b -> b) -> b -> [a] -> b foldr f s [] = s foldr f s (x:xs) = f x (foldr f s xs)

© M. Winter COSC 4P41 – Functional Programming Folding Binary (associative) operation op on a type a with neutral element e (Examples: (+,0), (*,1), (++,[]), (&&,True), (||,False), (.,id) ) foldr op e [a 0,a 1,…,a n-1,a n ] ( or foldl ) = a 0 `op` a 1 `op` … `op` a n-1 `op` a n foldr op e [] = e Binary (associative) operation op on a type a (without a neutral element) (Examples: max, min, lcm, gcd ) foldr1 op [a 0,a 1,…,a n-1,a n ] ( or foldl ) = a 0 `op` a 1 `op` … `op` a n-1 `op` a n foldr1 op [] is not defined (causes an error) If op is not associative the foldl and foldr versions may result in different functions.

© M. Winter COSC 4P41 – Functional Programming Folding Binary function with arguments from different types Examples: reverse :: [a] -> [a] reverse = foldl (flip (:)) [] sort :: Ord a => [a] -> [a] sort = foldr insert [] Notice that foldr (flip (:)) [] and foldl insert [] don’t work.

© M. Winter COSC 4P41 – Functional Programming Generalization Consider the following function: getWord :: String -> String getWord []= [] getWord (x:xs) | isSpace x= [] | otherwise= x : getWord xs We can generalize this function to have a test – or property – as a parameter. getUntil :: (a -> Bool) -> [a] -> [a] getUntil … getWord = getUntil isSpace

© M. Winter COSC 4P41 – Functional Programming Functions as values Function composition (.) :: (b -> c) -> (a -> b) -> (a -> c) (f. g) x = f (g x) infixl 9 >.> (>.>) :: (a -> b) -> (b -> c) -> (a -> c) g >.> f = f. g

© M. Winter COSC 4P41 – Functional Programming Lambda notation Instead of naming and defining a function, we can write it down directly. \n -> 2*n The following definitions of f are equivalent: f x y = result f = \x y -> result Example: multiples :: Int -> [Int] -> [Int] multiples n = filter (\x -> x `mod` n == 0)

© M. Winter COSC 4P41 – Functional Programming Example: creating an index Example: Input: ”cathedral doggerel cathedral\nbattery doggerel cathedral\n cathedral” Output: battery2 cathedral1, 2, 3 doggerel1, 2 makeIndex :: Doc -> [([Int],Word)] type Doc= String type Line= String type Word= String

© M. Winter COSC 4P41 – Functional Programming Example (cont’d) makeIndex = lines >.>-- Doc-> [Line] numLines >.>-- [Line]-> [(Int,Line)] allNumWords >.>-- [(Int,Line)]-> [(Int,Word)] sortLs >.>-- [(Int,Word)] -> [(Int,Word)] makeLists >.>-- [(Int,Word)]-> [([Int],Word)] amalgamate >.>-- [([Int],Word)]-> [([Int],Word)] shorten-- [([Int],Word)]-> [([Int],Word)]