lecture 20: lists and higher-order functions in haskell
Post on 05-Feb-2016
55 Views
Preview:
DESCRIPTION
TRANSCRIPT
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
11
Lecture 20: Lists and Higher-Order Lecture 20: Lists and Higher-Order Functions in HaskellFunctions in Haskell
COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts
Spring 2002Spring 2002
Felix Hernandez-CamposFelix Hernandez-Campos
Feb 27Feb 27
The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
22
List ComprehensionsList Comprehensions
• Lists can be defined by enumeration using Lists can be defined by enumeration using list list comprehensionscomprehensions
– Syntax: Syntax: [ f x | x <- xs ]
[ (x,y) | x <- xs, y <- ys ]
• ExampleExamplequicksort [] = []
quicksort (x:xs) = quicksort [y | y <- xs, y<x ]
++ [x]
++ quicksort [y | y <- xs, y>=x]
GeneratorGenerator
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
33
Arithmetic SequencesArithmetic Sequences
• Haskell support a special syntax for arithmetic Haskell support a special syntax for arithmetic sequencessequences
– Notation: [start, next element..end]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)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
44
Lists as Data TypesLists 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)data List a = Nil | Cons a (List a)
[ ][ ] ::
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
55
List OperationsList Operations
• ConcatenationConcatenation(++) :: [a] -> [a] -> [a](++) :: [a] -> [a] -> [a]
[] ++ ys = ys[] ++ ys = ys (1)(1)(x : xs) ++ ys = x : (xs ++ ys)(x : xs) ++ ys = x : (xs ++ ys) (2)(2)
– Example Example [1, 2] ++ [3, 4] [1, 2] ++ [3, 4] [1, 2, 3, 4][1, 2, 3, 4]
1:2:[] ++ 3:4:[]1:2:[] ++ 3:4:[]= { definition (2) }= { definition (2) }
1:(2:[] ++ 3:4:[])1:(2:[] ++ 3:4:[])= { definition (2) }= { definition (2) }
1: 2:([] ++ 3:4:[])1: 2:([] ++ 3:4:[])= { definition (1) }= { definition (1) }
1:2:3:4:[]1:2:3:4:[]
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
66
List OperationsList Operations
• ConcatConcat
concat :: [[a]] -> [a]concat :: [[a]] -> [a]concat [] = []concat [] = []concat (xs : xss) = xs ++ concat xssconcat (xs : xss) = xs ++ concat xss
– ExampleExample
concat [[1],[],[2,3,4]]concat [[1],[],[2,3,4]] [1,2,3,4][1,2,3,4]
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
77
List OperationsList Operations
• ReverseReverse
reverse :: [a] -> [a]reverse :: [a] -> [a]reverse [] = []reverse [] = []reverse (x : xs) = reverse xs ++ [x]reverse (x : xs) = reverse xs ++ [x]
– ExampleExample
reverse [1,2,3,4]reverse [1,2,3,4] [4,3,2,1][4,3,2,1]
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
88
Higher-Order FunctionsHigher-Order Functions
• Higher-order functions are functions that take other Higher-order functions are functions that take other functions as argumentsfunctions as arguments
• They can be use to implement algorithmicThey can be use to implement algorithmic skeletons skeletons– Generic algorithmic techniquesGeneric algorithmic techniques
• Three predefined higher-order functions are specially Three predefined higher-order functions are specially useful for working with listuseful for working with list– mapmap– foldfold– filterfilter
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
99
MapMap
• Applies a function to all the elements of a listApplies a function to all the elements of a listmap :: (a -> b) -> [a] -> [b]map :: (a -> b) -> [a] -> [b]map f [] = []map f [] = []map f (x : xs) = f x : map f xsmap f (x : xs) = f x : map f xs
– ExamplesExamples
map square [9, 3] map square [9, 3] [81, 9][81, 9]
map (<3) [1, 5] map (<3) [1, 5] [True, False][True, False]
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1010
FilterFilter
• Extracts the elements of a list that satisfy a boolean Extracts the elements of a list that satisfy a boolean functionfunction
filter :: (a -> Bool) -> [a] -> [a] filter :: (a -> Bool) -> [a] -> [a] filter p [] = []filter p [] = []filter p (x : xs) = if p x then x : filter p xsfilter p (x : xs) = if p x then x : filter p xs
else filter p xselse filter p xs
– ExampleExample
filter (>3) [1, 5, -5, 10, -10] filter (>3) [1, 5, -5, 10, -10] [5, [5, 10]10]
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1111
FoldFold
• Takes in a function and Takes in a function and foldsfolds it in between the it in between the elements of a listelements of a list
• Two flavors:Two flavors:– Right-wiseRight-wise fold: [x fold: [x11, x, x22, x, x33] ] xx11 (x (x22 (x (x3 3 e)) e))
Fold OperatorFold Operator Base ElementBase Element
foldr :: (a -> b -> b) -> b -> [a] -> [a] foldr :: (a -> b -> b) -> b -> [a] -> [a] foldr f e [] = []foldr f e [] = []foldr f e (x:xs) = f x (foldr f e xs)foldr f e (x:xs) = f x (foldr f e xs)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1212
FoldrFoldrExamplesExamples
• The algorithmic skeleton defined by foldr is very The algorithmic skeleton defined by foldr is very powerfulpowerful
• We can redefine many functions seen so far using We can redefine many functions seen so far using foldrfoldrconcat :: [[a]] -> [a]concat :: [[a]] -> [a]
concat [] = []concat [] = []concat (xs : xss) = xs ++ concat xssconcat (xs : xss) = xs ++ concat xss
concat = foldr (++) []concat = foldr (++) []
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1313
FoldrFoldrExamplesExamples
lengthlength :: [a] -> Int :: [a] -> Int
length [] = 0length [] = 0length (x : xs) = 1 + length xslength (x : xs) = 1 + length xs
length = foldr oneplus 0length = foldr oneplus 0where oneplus x n = 1 + nwhere oneplus x n = 1 + n
mapmap :: (a -> b) -> [a] -> [b] :: (a -> b) -> [a] -> [b]
map f [] = []map f [] = []map f (x : xs) = f x : map f xsmap f (x : xs) = f x : map f xs
map f = foldr (cons map f = foldr (cons . f) []. f) []where cons x xs = x : xswhere cons x xs = x : xs
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1414
CompositionComposition
• In the previous example, we used an important In the previous example, we used an important operator, operator, function compositionfunction composition
• It is defined as follows:It is defined as follows:
(.) :: (b -> c) -> (a -> b) -> (a -> c)(.) :: (b -> c) -> (a -> b) -> (a -> c)
(f . g) x = f (g x)(f . g) x = f (g x)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1515
FoldlFoldl
• Left-wiseLeft-wise fold: [x fold: [x11, x, x22, x, x33] ] ((e ((e x x11) ) x x22) ) x x33
foldl :: (a -> b -> b) -> b -> [a] -> [a] foldl :: (a -> b -> b) -> b -> [a] -> [a]
foldl f e [] = []foldl f e [] = []
foldl f e (x:xs) = foldl f (f e x) xsfoldl f e (x:xs) = foldl f (f e x) xs
• ExampleExamplemax a b = if a > b then a else bmax a b = if a > b then a else b
foldl max 0 [1,2,3] foldl max 0 [1,2,3] 33
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1616
Foldr and FoldlFoldr and Foldl
reverse :: [a] -> [a]reverse :: [a] -> [a]
reverse [] = []reverse [] = []reverse (x : xs) = reverse xs ++ [x]reverse (x : xs) = reverse xs ++ [x]
reverser = foldr snoc []reverser = foldr snoc [] where snoc x xs = xs ++ [x]where snoc x xs = xs ++ [x]
reversel = foldl cons []reversel = foldl cons [] where cons xs x = x : xswhere cons xs x = x : xs
• How can rewrite reverse to be O(n)?How can rewrite reverse to be O(n)?
O(nO(n22))
O(n)O(n)
O(nO(n22))
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1717
SolutionSolution
rev :: [a] -> [a]rev :: [a] -> [a]
rev xs = rev2 xs []rev xs = rev2 xs []
rev2 :: [a] -> [a] -> [a]rev2 :: [a] -> [a] -> [a]
rev2 []rev2 [] ys = ysys = ys
rev2 (x:xs) ys = (rev2 xs) (x:ys)rev2 (x:xs) ys = (rev2 xs) (x:ys)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1818
Reading AssignmentReading Assignment
• A Gentle Introduction to HaskellA Gentle Introduction to Haskell by Paul Hudak, by Paul Hudak, John Peterson, and Joseph H. Fasel.John Peterson, and Joseph H. Fasel.
– http://www.haskell.org/tutorial/http://www.haskell.org/tutorial/– Read sections 3 and 4 (intro, 4.1-3)Read sections 3 and 4 (intro, 4.1-3)
top related