1 / 8

Patterns of computation over lists

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

Download Presentation

Patterns of computation over lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

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

  3. 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.

  4. 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

  5. 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

  6. 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)

  7. Example: creating an index Example: Input: ”cathedral doggerel cathedral\nbattery doggerel cathedral\n cathedral” Output: battery 2 cathedral 1, 2, 3 doggerel 1, 2 makeIndex :: Doc -> [([Int],Word)] type Doc = String type Line = String type Word = String

  8. 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)]

More Related