Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts."— Presentation transcript:

1 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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. –http://www.haskell.org/tutorial/ http://www.haskell.org/tutorial/ –Read sections 3 and 4 (intro, 4.1-3)


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

Similar presentations


Ads by Google