7
7/23/2019 S12P2Fold Example http://slidepdf.com/reader/full/s12p2fold-example 1/7 !"#!$$% "'()'*++,-) "*'*.,)+/ 0'1 2(34'5 !(66,4' #7',-) 89:; <(6. =>*+764 ?@*/A466B !"#$%$&# () *+,-. *%-.,+ 

S12P2Fold Example

Embed Size (px)

Citation preview

Page 1: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 1/7

!"#!$$%"'()'*++,-) "*'*.,)+/

0'1 2(34'5 !(66,4'

#7',-) 89:;

<(6. =>*+764

?@*/A466B!"#$%$&# ()

*+,-. *%-.,+ 

Page 2: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 2/7

8

@,)C4'DE'.4' <F-G5,(-/ *-. <(6.,-)

!"#$% ?H'(+ "'46F.4B ,/ * @,)C4'DE'.4' <F-G5,(- ,5 5*A4/ * 24.FG5,(- ?*-. *- I.4-5,5JB */ *- K')F+4-5

5C4 % ,- !"#$% +4*-/ <(6.,-) 5( 5C4 2,)C5

@(L ,/ ,5 I+764+4-54.M

!"#$% '' () *+ , *+ ,- *+ , *+ .)/ *+ ,!"#$% "0 1$ ./ 2 1$

!"#$% "0 1$ (3'4-

2 ("0 3 (!"#$% "0 1$ 4--

Page 3: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 3/7

N

I-/4'5,(- #('5 24O,/,54.

@(L .( J(F I+764+4-5 I-/4'5,(- #('5 P/,-) !"#$%M

1567%4 '' 854 *+ .854/ *+ .854/

1567%4 9 ./ 2 .9/

1567%4 9 (3'4-

: 9 ;2 3 2 9'(3'4-

: "437%<167 2 3'(1567%4 9 4-

1567%4="%4> '' .854/ *+ .854/

1567%4="%4> 9 2 !"#$% 1567%4 ./ 9

Page 4: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 4/7

$

I-/4'5,(- #('5 24O,/,54.

1567%4="%4> .?@A@B/C+ !"#$% 1567%4 ./ .?@A@B/

C+ 1567%4 ? (!"#$% 1567%4 ./ .A@B/-

C+ 1567%4 ? (1567%4 A (!"#$% 1567%4 ./ .B/--

C+ 1567%4 ? (1567%4 A (1567%4 B (!"#$%1567%4 ./ ./---

C+ 1567%4 ? (1567%4 A (1567%4 B (./---

C+ 1567%4 ? (1567%4 A (.B/--

C+ 1567%4 ? (.B@A/-C+ B'(1567%4 ? .A/-

C+ DDD

C+ .B@A@?/

>> 1:2:(insert 3 [])

>> 1:2:3:[]

Page 5: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 5/7

;

@,)C4'DE'.4' <F-G5,(-/ *-. <(6.,-)

!"#$# ?H'(+ "'46F.4B ,/ * @,)C4'DE'.4' <F-G5,(- ,5 5*A4/ * 24.FG5,(- ?*-. *- I.4-5,5JB */ *- K')F+4-5

5C4 # ,- !"#$# +4*-/ <(6.,-) 5( 5C4 Q4H5

@(L ,/ ,5 I+764+4-54.M

!"#$# '' () *+ , *+ ,- *+ , *+ .)/ *+ ,!"#$# "0 E ./ 2 E

!"#$# "0 1$ (3'4-

2 !"#$# "0 ("0 1$ 3- 4

Here, the base case involves the leftmost element.

The first thing evaluated is the operator and the head of the list.

If the list is empty, the result is the initial value z.

If not, fold the tail of the list using as new initial value which is

the result of applying op to the old initial value and the first element.

Page 6: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 6/7

!(+7*',/(-

!"#$% "0% 1$ )','F'$'7'G 790)5$6 4"

) H"0%H (, H"0%H (F H"0%H (DDD H"0H 1$---

!"#$# "0# 1$ )','F'$'7'G 790)5$6 4"

(((1$ H"0#H )- H"0#H ,- H"0#H F- H"0#H DDD

R

opr a (opr b (opr c (. . . opr id . . .)))

Page 7: S12P2Fold Example

7/23/2019 S12P2Fold Example

http://slidepdf.com/reader/full/s12p2fold-example 7/7

!(+7*',/(-

S,O4- *- ,-T-,54 6,/5 */ ,-7F5U

H(6.' C*/ * GC*-G4 (H 54'+,-*5,-) ,H V(7W G*- 54'+,-*54

(- *- ,-T-,54 6,/5

H(6.6 L,66 -4O4' 54'+,-*54 /,-G4 5C4 '4GF'/,(- .474-./

(- H(6.6 *-. 5C4 ,-7F5 6,/5 '4)*'.64// (H 5C4 V(7W

X