1 00:00:07,880 --> 00:00:12,510 Welcome back, you've seen the accumulator pattern before. 2 00:00:12,510 --> 00:00:16,990 Now, we're going to accumulate a list, instead of a number. 3 00:00:17,480 --> 00:00:20,250 So in this code example, 4 00:00:20,250 --> 00:00:22,200 as with all accumulator patterns, 5 00:00:22,200 --> 00:00:25,245 we have to have some sequence that we're going to generate over. 6 00:00:25,245 --> 00:00:27,760 In this case, we have 7 00:00:28,070 --> 00:00:39,790 variable nums and we are binding to a list containing three, five, and eight. 8 00:00:43,250 --> 00:00:47,110 We're now going to accumulate a list. 9 00:00:47,110 --> 00:00:52,405 We'd like that list to contain the squares of all the numbers from the original list; 10 00:00:52,405 --> 00:00:56,930 so nine, 25, and 64. 11 00:00:58,550 --> 00:01:02,360 As with all accumulated patterns, 12 00:01:02,360 --> 00:01:04,465 you have some accumulator variable. 13 00:01:04,465 --> 00:01:07,900 I'm going to call it accum. 14 00:01:08,430 --> 00:01:12,825 We could call it something else, maybe squares, 15 00:01:12,825 --> 00:01:18,225 would probably be a reasonable choice. 16 00:01:18,225 --> 00:01:25,310 Anyway, it's called accum here and we initialize it to be just an empty list. 17 00:01:27,620 --> 00:01:31,405 This is typical when we're accumulating list. 18 00:01:31,405 --> 00:01:35,210 We start with an empty list and we're going keep appending to it. 19 00:01:36,440 --> 00:01:39,670 This is the analog of starting with zero. 20 00:01:39,670 --> 00:01:43,315 When you're going to take an accumulation that gives you a sum, 21 00:01:43,315 --> 00:01:46,480 we start with the empty list and then on line three, 22 00:01:46,480 --> 00:01:48,219 we start our iteration. 23 00:01:48,219 --> 00:01:55,520 So we're going to iterate each time w is going to be bound to one of the items from nums. 24 00:01:55,520 --> 00:01:58,350 In the first iteration, 25 00:01:58,350 --> 00:02:00,375 w will be bound to three. 26 00:02:00,375 --> 00:02:08,380 We'll set x to be the square of three or nine and 27 00:02:08,380 --> 00:02:17,110 then we're going to append the current value of x nine into the accumulator. 28 00:02:17,120 --> 00:02:20,580 On the second iteration, 29 00:02:20,580 --> 00:02:30,590 w gets bound to the second value five and so x gets 30 00:02:30,590 --> 00:02:34,550 bound to 25 and we 31 00:02:34,550 --> 00:02:41,675 append 25 the third time. 32 00:02:41,675 --> 00:02:46,385 W is eight, x is 64. 33 00:02:46,385 --> 00:02:52,420 Sixty four gets appended to the list. 34 00:02:52,950 --> 00:02:57,580 So this is following the same logic of your accumulator pattern 35 00:02:57,580 --> 00:03:03,440 before each time through this iteration. 36 00:03:03,440 --> 00:03:08,470 An additional item is going to be appended into accum and so really 37 00:03:08,470 --> 00:03:13,735 accum is the squares so far that we've managed to accumulate. 38 00:03:13,735 --> 00:03:15,430 When we're done with this iteration, 39 00:03:15,430 --> 00:03:22,040 we are finished with all three of these, 40 00:03:22,040 --> 00:03:27,265 can printout accum, and accum will have all of the squares. 41 00:03:27,265 --> 00:03:32,460 So let's run it and sure enough we get nine, 42 00:03:32,460 --> 00:03:36,130 25 and 64 at the end. 43 00:03:37,550 --> 00:03:40,860 So it was a little check on your understanding. 44 00:03:40,860 --> 00:03:44,160 Let me try changing one thing in this, 45 00:03:44,160 --> 00:03:48,085 suppose that I indent that print. 46 00:03:48,085 --> 00:03:51,980 Can you predict what the output will be? 47 00:03:52,740 --> 00:03:57,850 A good chance to pause and see if you can predict it. 48 00:03:57,850 --> 00:04:03,385 At each stage, we're now going to not just 49 00:04:03,385 --> 00:04:06,010 change the contents of the accumulator but we're going 50 00:04:06,010 --> 00:04:09,085 to print what we've accumulated so far. 51 00:04:09,085 --> 00:04:12,170 So we're now going to have three lines of output. 52 00:04:12,170 --> 00:04:16,614 First just the list nine and then list with nine and 25, 53 00:04:16,614 --> 00:04:20,420 and the list with nine, 25, and 64. 54 00:04:20,620 --> 00:04:26,360 So as before, this is a useful debugging strategy with 55 00:04:26,360 --> 00:04:29,450 the accumulator pattern is to print out what you've accumulated so 56 00:04:29,450 --> 00:04:33,560 far and see how it grows over time. 57 00:04:35,000 --> 00:04:38,845 So in summary, to accumulate later list, 58 00:04:38,845 --> 00:04:42,715 you will initialize the accumulator to be an empty list. 59 00:04:42,715 --> 00:04:45,170 You'll iterate and at each iteration, 60 00:04:45,170 --> 00:04:46,570 you will append a new item. 61 00:04:46,570 --> 00:04:48,180 At the end, volla, 62 00:04:48,180 --> 00:04:54,100 you've have accumulated a new list. Catch you later.