Fold Operations - PowerPoint PPT Presentation

About This Presentation
Title:

Fold Operations

Description:

Motivation : Appending lists. lappend (link) generalization of binary append to appending an arbitrary number of lists. fun lappend ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 20
Provided by: csWr
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Fold Operations


1
Fold Operations
  • Abstracting Repetitions

2
Motivation Appending lists
  • lappend (link)
  • generalization of binary append to appending an
    arbitrary number of lists
  • fun lappend
  • lappend (sss) s _at_ (lappend ss)
  • lappend a list list -gt a list

3
Properties (Identities)
  • (map f) o lappend
  • lappend o (map (map f))
  • filter p (xs _at_ ys)
  • (filter p xs) _at_ (filter p ys)
  • (filter p) o lappend
  • lappend o (map (filter p))

4
Fold Operators
  • Generalize binary operators to n-ary functions
    (list functions).
  • Abstract specific patterns of recursion / looping
    constructs.
  • Potential for optimization of special forms.
  • foldl, accumulate, revfold,
  • foldr, reduce, fold,
  • foldl1
  • foldr1

5
Foldr (SML97)
  • foldr f a x1,x2,,xn
  • f( x1 , f(x2, , f(xn,a)...))
  • fun foldr f a a
  • foldr f a (xxs)
  • f (x, foldr f a xs)
  • foldr (ab -gt b) -gtb -gt
  • a list -gt b
  • foldr (op ) 10 1,2,3 16

6
Examples
  • foldr (op ) a x1,x2,x3
  • ( x1 ( x2 ( x3 a) ) ) )
  • foldr (op ) 1,0 4,3,2
  • 4,3,2,1,0

7
(contd)
  • fold in OldML (not suited for partial eval.)
  • fold (ab -gt b) -gt
  • a list -gtb -gt b
  • foldr in Bird and Wadler (Curried func.)
  • reduce in Reade
  • foldr (a -gt b -gt b) -gt b -gt
  • a list -gt b

8
Foldl (SML97)
  • foldl f a x1,x2,,xn
  • f(xn,f(,f(x2,f(x1,a))))
  • fun foldl f a a
  • foldl f a (xxs)
  • foldl f (f(x,a)) xs
  • foldl (ab -gt b) -gtb -gt
  • a list -gt b
  • foldl (op ) 10 1,2,3 60

9
Examples
  • foldl (op ) a x1,x2,,xn
  • (xn ((x2 (x1 a))...))
  • foldl (op _at_) 0 1,2,3,4
  • 4,3,2,1,0
  • foldl (op ) 1,0 4,3,2
  • 2,3,4,1,0

10
(contd)
  • revfold in OldML (not suited for partial eval.)
  • revfold (ba -gt b) -gt
  • a list -gtb -gt b
  • foldl in Bird and Wadler (Curried func.)
  • accumulate in Reade
  • foldl (b -gt a -gt b) -gt b -gt
  • a list -gtb

11
Examples
  • fun pack ds
  • foldl (fn (d,v)gt dv10) 0 ds
  • pack 1,2,3,4 1234
  • fun packNot ds
  • foldl (fn (d,v)gt d10v) 0 ds
  • packNot 1,2,3,4 100
  • fun packNott ds
  • foldr (fn (d,v)gt dv10) 0 ds
  • packNott 1,2,3,4 4321

12
  • fun myId lis
  • foldr (fn (x,xs) gt xxs) lis
  • myId 1,2,3,4 1,2,3,4
  • fun myRev lis
  • foldl (fn (x,xs) gt xxs) lis
  • myRev 1,2,3,4 4,3,2,1

13
  • fun filter p lis
  • foldr (fn (x,xs) gt if p x
  • then xxs else xs)
  • lis
  • filter (fn x gt x 2) 1,2,3,2
  • fun filterNeg p lis
  • foldl (fn (x,xs) gt if p x
  • then xs else xs_at_x)
  • lis
  • filterNeg (fn x gt true) a,b,a

14
  • fun takewhile p lis
  • foldr (fn (x,xs) gt if p x
  • then xxs else )
  • lis
  • takewhile (fn x gt x 2) 2,2,3,1,2
  • fun dropwhile p lis
  • foldl (fn (x,xs) gt
  • if null xs andalso p x
  • then xs else xs_at_x)
  • lis
  • dropwhile (fn x gt true) 1,2,3

15
Generalizing Operators without identity element
  • E.g., max, min, etc for which basis clause (for
    ) cannot be defined.
  • fun foldl_1 f (xxs)
  • foldl f x xs
  • fun foldr_1 f (x) x
  • foldr_1 f (xyys)
  • f x (foldr_1 f (yys))

16
Laws Identities
  • If f is a binary function that is associative and
    a is an identity w.r.t. f, then
  • foldr f a xs
  • foldl f a (rev xs)
  • foldr (op _at_) 1,2,3,4,5
  • 1,2,3,4,5
  • foldl (op _at_) (rev 1,2,3,4,5)
  • 1,2,3,4,5
  • foldl (op _at_) 1,2,3,4,5
  • 5,3,4,1,2

17
Laws Identities
  • If f is a binary function that is commutative
    and a is an identity w.r.t. f, then
  • foldr f a xs foldl f a xs
  • foldr (op ) 1 1,2,3 6
  • foldl (op ) 1 1,2,3 6
  • foldr (fn(b,v)gt b andalso v) true false
  • false
  • foldl (fn(b,v)gt b andalso v) true false
  • false

18
Comparing foldl and foldr.
  • foldl (op ) 0 1,2,3
  • foldl (op ) 1 2,3
  • foldl (op ) 3 3
  • foldl (op ) 6
  • 6
  • (foldl Efficient)
  • (Tail Recursive)
  • foldr (op ) 0 1,2,3
  • 1foldr (op ) 0 2,3
  • 3 foldr (op ) 0 3
  • 6 foldr (op ) 0
  • 6
  • foldl and t t,f,t
  • foldl and t f,t
  • foldl and f t
  • foldl and f
  • false
  • (foldr Efficient)
  • (Short-circuit evaluation)
  • foldr and t t,f,t
  • and t
  • (foldr and t f,t
  • and f
  • (foldr and t t)
  • false

19
scan (processing prefixes)
  • scan f a x1,x2,,xn
  • a,(f a x1),(f (f a x1) x2),,
  • (f (f (f a x1) x2) xn)
  • fun scan f a a
  • scan f a xs
  • let
  • val t (scan f a (init xs))
  • in
  • t _at_ (f (last t) (last xs))
  • end
Write a Comment
User Comments (0)
About PowerShow.com