1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.

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.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
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.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,
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 21: Functional Programming in Python COMP 144 Programming Language Concepts Spring.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 1: Introduction COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Advanced Programming Languages Lecture 1 NCCU CS Dept. Fall 2005 Sept. 19, 2005.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
Chapter 5 Polymorphic and Higher-Order Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
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.
Lazy Functional Programming in Haskell
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.
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
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,
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.
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.
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.
Lecture 14: Advanced Topic: Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Recursion.
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
PROGRAMMING IN HASKELL
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Feb 27 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos List Comprehensions Lists can be defined by enumeration using list comprehensionsLists can be defined by enumeration using list comprehensions –Syntax: [ f x | x <- xs ] [ (x,y) | x <- xs, y <- ys ] ExampleExample quicksort [] = [] quicksort (x:xs) = quicksort [y | y <- xs, y<x ] ++ [x] ++ quicksort [y | y =x]Generator

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Arithmetic Sequences Haskell support a special syntax for arithmetic sequencesHaskell support a special syntax for arithmetic sequences –Notation: [start, next element..end] [1..10]  [1,2,3,4,5,6,7,8,9,10] [1,3..10]  [1,3,5,7,9] [1,3..]  [1,3,5,7,9,… (infinite sequence)

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists as Data Types List can be seen as the following data types:List can be seen as the following data types: data List a = Nil | Cons a (List a) [ ] :

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos List Operations ConcatenationConcatenation (++) :: [a] -> [a] -> [a] [] ++ ys = ys (1) (x : xs) ++ ys = x : (xs ++ ys) (2) –Example [1, 2] ++ [3, 4] [1, 2, 3, 4] –Example [1, 2] ++ [3, 4]  [1, 2, 3, 4] 1:2:[] ++ 3:4:[] = { definition (2) } 1:(2:[] ++ 3:4:[]) = { definition (2) } 1: 2:([] ++ 3:4:[]) = { definition (1) } 1:2:3:4:[]

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos List Operations ConcatConcat concat :: [[a]] -> [a] concat [] = [] concat (xs : xss) = xs ++ concat xss –Example concat [[1],[],[2,3,4]] [1,2,3,4] concat [[1],[],[2,3,4]]  [1,2,3,4]

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos List Operations ReverseReverse reverse :: [a] -> [a] reverse [] = [] reverse (x : xs) = reverse xs ++ [x] –Example reverse [1,2,3,4] [4,3,2,1] reverse [1,2,3,4]  [4,3,2,1]

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Higher-Order Functions Higher-order functions are functions that take other functions as argumentsHigher-order functions are functions that take other functions as arguments They can be use to implement algorithmic skeletonsThey can be use to implement algorithmic skeletons –Generic algorithmic techniques Three predefined higher-order functions are specially useful for working with listThree predefined higher-order functions are specially useful for working with list –map –fold –filter

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Map Applies a function to all the elements of a listApplies a function to all the elements of a list map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x : xs) = f x : map f xs –Examples map square [9, 3] [81, 9] map square [9, 3]  [81, 9] map (<3) [1, 5] [True, False] map (<3) [1, 5]  [True, False]

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Filter Extracts the elements of a list that satisfy a boolean functionExtracts the elements of a list that satisfy a boolean function filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x : xs) = if p x then x : filter p xs else filter p xs –Example filter (>3) [1, 5, -5, 10, -10] [5, 10] filter (>3) [1, 5, -5, 10, -10]  [5, 10]

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Fold Takes in a function and folds it in between the elements of a listTakes in a function and folds it in between the elements of a list Two flavors:Two flavors: –Right-wise fold: [x 1, x 2, x 3 ] x 1  (x 2  (x 3  e)) –Right-wise fold: [x 1, x 2, x 3 ]  x 1  (x 2  (x 3  e)) Fold Operator Base Element foldr :: (a -> b -> b) -> b -> [a] -> [a] foldr f e [] = [] foldr f e (x:xs) = f x (foldr f e xs)

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Foldr Examples The algorithmic skeleton defined by foldr is very powerfulThe algorithmic skeleton defined by foldr is very powerful We can redefine many functions seen so far using foldrWe can redefine many functions seen so far using foldr concat :: [[a]] -> [a] concat [] = [] concat (xs : xss) = xs ++ concat xss concat = foldr (++) []

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Foldr Examples length :: [a] -> Int length [] = 0 length (x : xs) = 1 + length xs length = foldr oneplus 0 where oneplus x n = 1 + n map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x : xs) = f x : map f xs map f = foldr (cons. f) [] where cons x xs = x : xs

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Composition In the previous example, we used an important operator, function compositionIn the previous example, we used an important operator, function composition It is defined as follows:It is defined as follows: (.) :: (b -> c) -> (a -> b) -> (a -> c) (f. g) x = f (g x)

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Foldl Left-wise fold: [x 1, x 2, x 3 ] ((e  x 1 )  x 2 )  x 3Left-wise fold: [x 1, x 2, x 3 ]  ((e  x 1 )  x 2 )  x 3 foldl :: (a -> b -> b) -> b -> [a] -> [a] foldl f e [] = [] foldl f e (x:xs) = foldl f (f e x) xs ExampleExample max a b = if a > b then a else b foldl max 0 [1,2,3] 3 foldl max 0 [1,2,3]  3

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Foldr and Foldl reverse :: [a] -> [a] reverse [] = [] reverse (x : xs) = reverse xs ++ [x] reverser = foldr snoc [] where snoc x xs = xs ++ [x] where snoc x xs = xs ++ [x] reversel = foldl cons [] where cons xs x = x : xs where cons xs x = x : xs How can rewrite reverse to be O(n)?How can rewrite reverse to be O(n)? O(n 2 ) O(n)

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Solution rev :: [a] -> [a] rev xs = rev2 xs [] rev2 :: [a] -> [a] -> [a] rev2 []ys = ys rev2 (x:xs) ys = (rev2 xs) (x:ys)

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment A Gentle Introduction to Haskell by Paul Hudak, John Peterson, and Joseph H. Fasel.A Gentle Introduction to Haskell by Paul Hudak, John Peterson, and Joseph H. Fasel. – –Read sections 3 and 4 (intro, 4.1-3)