1 / 9

Folding

Folding. foldr and foldl applied to addition (using infix notation). First some examples Then proofs of the types of foldr and foldl. foldr (+) 0 [2,3,4].

debbiehart
Télécharger la présentation

Folding

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. Folding foldr and foldl applied to addition (using infix notation)

  2. First some examples • Then proofs of the types of foldr and foldl

  3. foldr (+) 0 [2,3,4] foldr (+) 0 [2,3,4] = (recursion)(+) 2 (foldr (+) 0 [3,4]) = (recursion)(+) 2 ( (+) 3 (+) (foldr (+) 0 [4]) ) = (recursion)(+) 2 ( (+) 3 ( (+) (4 (+) (foldr (+) 0 []) ))) = (base)(+) 2 ( (+) 3 ( (+) (4 (+) (0) ))) = (infix notation)This computes(2 + (3 + (4 + 0))) = 9

  4. foldl (+) 0 [2,3,4] foldl (+) 0 [2,3,4] = (recursion)foldl(+) ((+) 0 2) [3,4] =foldl(+) (2 [3,4]) = (recursion)foldl (+) ((+) 2 3) [4]) = foldl (+) (5 [4]) = (recursion) foldl (+) ((+) 5 4) [] = (base) foldl (+) 9 [] = 9This computes (((0(+)2)(+)3)(+)4) = 9

  5. type of foldl / foldr • As applied to (+), both functions have the type:

  6. type of foldl / foldr • As applied to (+), both functions have the type: (Int -> Int -> Int) -> (Int -> [Int] -> Int) • The lecture notes show that this is a special case. In general, foldr :: (a->b->b) - > b->[a]->b foldl :: (a->b->a) -> a->[b]->a

  7. First foldr, starting from its definition • The first question to ask is, what is the type of f • More specifically, what’s the type of the outcome of f? (Does this outcome have the same type as f’s 1st argument or the same type as f’s 2nd argument?)

  8. R S R,S,P,Q are types foldr f y [] = y P Q foldr f y (x:xs) = f x (foldr f y xs) ?? [a] a b f :: a  b  ??=b because Q=P=b y :: ?? ??=b because S=P Therefore, foldr :: (abb)b[a]b

  9. Now foldl, starting from its definition foldl f y [] = y P and Q are types foldl f y (x:xs) = foldl f (fyx) xs P Q=a f :: a -> b -> ? Within fyx, the first arg of f is y, so Type(y)=a. P = Qtherefore, Type(y)=P=a Therefore (comparing left and right), Type(fyx)=a. Therefore, ?=a Therefore, Type(foldl) = (a->b->a) -> a->[b]->a

More Related