1 00:00:06,120 --> 00:00:10,292 Here's another Helper function role. It says that when we shift knowledge 2 00:00:10,292 --> 00:00:16,258 domain we should use a Helper function. That sounds kind of weird, but really all 3 00:00:16,258 --> 00:00:19,482 it's saying is that if we're in the middle of designing a function about one 4 00:00:19,482 --> 00:00:22,660 thing. Maybe this function is about apples. 5 00:00:23,780 --> 00:00:28,790 And then we need to do a very different kind of thing, maybe oranges. 6 00:00:28,790 --> 00:00:31,550 Then right there is where we should put helper function call. 7 00:00:32,820 --> 00:00:35,362 Maybe the apples and oranges doesn't explain it, but I think you can look at 8 00:00:35,362 --> 00:00:39,053 the examples in this video. You'll be able to understand when to use 9 00:00:39,053 --> 00:00:43,561 this rule. Now I'm working on finishing the insert 10 00:00:43,561 --> 00:00:46,110 function. We've already got the wishlist entry. 11 00:00:46,110 --> 00:00:48,450 So the next thing to do is to work on the examples. 12 00:00:48,450 --> 00:00:51,990 And at this point in this program, what I'm going to realize is, gee, you know? 13 00:00:51,990 --> 00:00:55,800 I've got a lot of examples involving images of different sizes. 14 00:00:55,800 --> 00:00:58,465 And they get kind of cumbersome to write, so I'm going to do something that I might 15 00:00:58,465 --> 00:01:01,639 have done earlier. It's fine that I didn't do it earlier, 16 00:01:01,639 --> 00:01:06,740 and I don't absolutely have to do it now. I'm just doing it so that you can see it 17 00:01:06,740 --> 00:01:09,170 happen. What I'm going to do is, I'm going to 18 00:01:09,170 --> 00:01:13,025 take these examples that I've been using. And I'm going to take the images that 19 00:01:13,025 --> 00:01:17,130 I've been using, and I'm going to turn them into named constants. 20 00:01:17,130 --> 00:01:20,040 So for example, this shows up in a lot of my examples. 21 00:01:20,040 --> 00:01:26,058 I'll just go up here to constants and I'll make a special section of them 22 00:01:26,058 --> 00:01:32,780 called for testing. And I'll say define i1 is this thing and 23 00:01:32,780 --> 00:01:39,446 then what I'll do is I'll say let's find every occurrence of this thing and we'll 24 00:01:39,446 --> 00:01:45,992 replace it with I1. We're not going to replace that one 25 00:01:45,992 --> 00:01:51,259 because that would be circular. But we will replace this one and this one 26 00:01:51,259 --> 00:01:56,700 and this one and this one and this one and this one and this one. 27 00:01:56,700 --> 00:02:03,850 There were a lot of them. Not that one. 28 00:02:03,850 --> 00:02:07,926 And then will do it again for the other ones. 29 00:02:07,926 --> 00:02:34,656 20, 30 solid red. And then 30, 40, solid green. 30 00:02:34,656 --> 00:02:38,122 [BLANK_AUDIO]. 31 00:02:38,122 --> 00:02:54,177 And one important thing here is I ran it, and unfortunately the program wasn't 32 00:02:54,177 --> 00:03:01,700 working. When I ran it, because we knew that we 33 00:03:01,700 --> 00:03:04,520 were in the middle of working and so I'm not certain that I didn't make any 34 00:03:04,520 --> 00:03:07,770 mistakes. But I did it so systematically using 35 00:03:07,770 --> 00:03:11,120 finer place that I probably didn't make any mistakes. 36 00:03:11,120 --> 00:03:14,230 Whenever you do something like this you want to at very least re run it. 37 00:03:14,230 --> 00:03:16,390 To make sure your program is still well formed. 38 00:03:19,700 --> 00:03:30,070 And what I probably would also do is take the trouble to redo the line breaking. 39 00:03:30,070 --> 00:03:34,046 And in some programming environments, you would get some more automatic support for 40 00:03:34,046 --> 00:03:37,741 this kind of thing. Basically you're taking a recurring 41 00:03:37,741 --> 00:03:41,610 constant expression and replacing it by a named constant. 42 00:03:41,610 --> 00:03:46,446 That's a refactoring that some tools will give you automatic support for. 43 00:03:46,446 --> 00:03:51,549 And that kind of support has been in programming environments for years and 44 00:03:51,549 --> 00:03:55,722 years and years. Larry McCenter put that in the inter less 45 00:03:55,722 --> 00:03:59,450 D programming environment in the '80s at least that long ago. 46 00:03:59,450 --> 00:04:06,553 Alright, now we're back to insert. So let's do some examples, so we're 47 00:04:06,553 --> 00:04:11,864 going to insert some image, well, now we've got names for images. 48 00:04:11,864 --> 00:04:16,792 So we'll insert I1 into, we need the base case example first so if you insert an 49 00:04:16,792 --> 00:04:22,500 image into an empty list. You know, there's only one place for it 50 00:04:22,500 --> 00:04:29,690 to go which is the beginning of the list. That's the result there. 51 00:04:32,540 --> 00:04:38,620 Now let's see. Another example is to insert I1 into a 52 00:04:38,620 --> 00:04:49,543 list that already has I2 and I3 in it. And in that case it goes right at the 53 00:04:49,543 --> 00:04:55,801 beginning. And now that I've got these nice named 54 00:04:55,801 --> 00:05:02,125 constants I can really do all three cases. 55 00:05:02,125 --> 00:05:11,625 That's also Insert i2 which goes in the middle. 56 00:05:11,625 --> 00:05:23,747 And will produce that result. And let's also insert i3. 57 00:05:23,747 --> 00:05:31,295 Which produces that result. So now we've kind of got the full space. 58 00:05:31,295 --> 00:05:35,579 It either goes at the beginning of an empty list or it goes at the beginning of 59 00:05:35,579 --> 00:05:39,876 a non-empty list. Or it's somewhere in the middle of a 60 00:05:39,876 --> 00:05:43,696 nonempty list, or at the end of a nonempty list. 61 00:05:43,696 --> 00:05:47,050 That looks pretty good. Now we've got some examples. 62 00:05:47,050 --> 00:05:50,248 Let's run them to make sure they're well formed. 63 00:05:50,248 --> 00:05:53,022 Oops. One of my example isn't well formed. 64 00:05:53,022 --> 00:05:57,870 huh, and since I copied and pasted, it's going to happen a bunch of times. 65 00:05:57,870 --> 00:06:04,071 There that empty. That's okay, we'll fix it quickly. 66 00:06:04,071 --> 00:06:07,926 Lets try it again. Okay now they're well formed but of 67 00:06:07,926 --> 00:06:13,243 course they're failing. Cause of the stub. 68 00:06:13,243 --> 00:06:25,371 Let's see, let's go get the template. And we'll copy the template down here. 69 00:06:25,371 --> 00:06:32,117 We'll comment out the stub, oh dear. When we, we're designing this we name the 70 00:06:32,117 --> 00:06:37,130 parameters lst and we tend to name them loi. 71 00:06:37,130 --> 00:06:41,588 There's two ways to fix this. One is to go ahead and name it lst 72 00:06:41,588 --> 00:06:45,735 throughout this function. The other way is to be more consistent 73 00:06:45,735 --> 00:06:49,570 with what we were doing in other functions... 74 00:06:49,570 --> 00:06:52,450 Both work, but I think what I'll do here is just tell, I'll take the approach of 75 00:06:52,450 --> 00:06:56,019 being more consistent. I'll change that to loi, and that to loi, 76 00:06:56,019 --> 00:07:00,555 and I'll do it to in the stub even though the stub has been commented out. 77 00:07:00,555 --> 00:07:05,535 Now, I just like being consistent. So now, loi has the right name, but I do 78 00:07:05,535 --> 00:07:13,050 need to rename the function itself and rename the natural recursion. 79 00:07:13,050 --> 00:07:16,350 And this is a function that takes two arguments, whereas the template of 80 00:07:16,350 --> 00:07:20,650 course, only takes one. So we have to add the second argument 81 00:07:20,650 --> 00:07:25,736 image, and remember if you go to the design recipes page. 82 00:07:25,736 --> 00:07:30,818 And you go to the data driven templates page, there's a rule that when designing 83 00:07:30,818 --> 00:07:36,131 functions that consume additional atomic parameters, the name of that parameter 84 00:07:36,131 --> 00:07:40,824 gets. Is added after every dot dot dot in the 85 00:07:40,824 --> 00:07:43,980 template. So, we gotta do that here, so let's see 86 00:07:43,980 --> 00:07:47,392 img is going to go there, and it's going to go there. 87 00:07:47,392 --> 00:07:56,770 And something has to happen here, because Insert wants two arguments. 88 00:07:56,770 --> 00:08:00,820 So, I'll just put ...img. You could put that. 89 00:08:00,820 --> 00:08:05,395 Sometimes I would just put that, but I'll put ...img in this case. 90 00:08:05,395 --> 00:08:09,383 Okay, they're both equivalent. Both are a note to myself saying, hey, 91 00:08:09,383 --> 00:08:14,290 remember, insert needs two arguments. So, now let's get going. 92 00:08:14,290 --> 00:08:18,292 In the base case, we're inserting image into an empty list. 93 00:08:18,292 --> 00:08:22,729 And that corresponds to this first example. 94 00:08:22,729 --> 00:08:31,546 And so, we just put this image on the front of an empty list, like that. 95 00:08:32,820 --> 00:08:36,005 The next three examples are telling me that sometimes the inserted image goes 96 00:08:36,005 --> 00:08:39,450 right at the front of the list and sometimes it doesn't. 97 00:08:41,210 --> 00:08:46,108 In this first case, i1, the image that we're trying to insert, is not larger 98 00:08:46,108 --> 00:08:52,530 than i2, and so that means that i1 has to go before i2. 99 00:08:52,530 --> 00:08:56,050 That's what it means for the items to be in increasing order of size. 100 00:08:56,050 --> 00:09:00,209 Because i2 is bigger than i1. And i2 has to be after i1. 101 00:09:00,209 --> 00:09:04,810 But in this blue case, we're trying to insert i2. 102 00:09:04,810 --> 00:09:10,406 And the first item in the list is i1. And i2 is bigger than i1. 103 00:09:10,406 --> 00:09:16,180 So that means i2 has to go after i1 somewhere. 104 00:09:16,180 --> 00:09:19,620 Again that's what it means for them to be sorted in increasing the order of size. 105 00:09:21,010 --> 00:09:26,218 And the same is true for this blue case, i3 is larger than i2, so i3's got to go 106 00:09:26,218 --> 00:09:35,993 somewhere after i2 in the list. So what these examples are telling me is 107 00:09:35,993 --> 00:09:39,586 that at the very least there's gotta be an F here. 108 00:09:39,586 --> 00:09:43,429 Because there is a case where i1, the inserted item, goes at the beginning of 109 00:09:43,429 --> 00:09:47,516 the list and there's cases, the i2 and i3 cases where the inserted item doesn't go 110 00:09:47,516 --> 00:09:52,805 at the beginning of the list. So there's gotta be an if. 111 00:09:52,805 --> 00:09:58,680 And if there's an if there has to be a question so what's the question? 112 00:09:58,680 --> 00:10:03,411 Well the question has to do with comparing the sizes of the image to be 113 00:10:03,411 --> 00:10:08,250 inserted, with the first item of the list. 114 00:10:09,380 --> 00:10:15,204 So we need to do something here, with these two things to know, is image bigger 115 00:10:15,204 --> 00:10:23,538 than the first thing in the list. And you might put a note to yourself here 116 00:10:23,538 --> 00:10:30,550 is bigger now the question is how we going to do that. 117 00:10:30,550 --> 00:10:33,558 Well you know we need to determine if the area is different so we're going to have 118 00:10:33,558 --> 00:10:37,711 to get, width and height of the image. And the width and height of the first 119 00:10:37,711 --> 00:10:40,437 thing on the list and, and we kind of have to determine their size their 120 00:10:40,437 --> 00:10:42,585 relative sizes. But thats kind of complicated. 121 00:10:42,585 --> 00:10:48,600 And not only is it kind of complicated, but its about a different kind of issue. 122 00:10:48,600 --> 00:10:52,740 This function is really about inserting into a sorted list. 123 00:10:53,810 --> 00:10:58,800 Whereas this little piece of code here is about comparing sizes. 124 00:10:58,800 --> 00:11:02,640 And sorting a list is one kind of knowledge and comparing sizes of images 125 00:11:02,640 --> 00:11:07,530 is a different kind of knowledge. So what we've got here is a knowledge 126 00:11:07,530 --> 00:11:11,191 domain shift. We're shifting from knowledge about 127 00:11:11,191 --> 00:11:14,612 sorting lists to knowledge about comparing the size of images. 128 00:11:14,612 --> 00:11:21,111 And there's a helper rule that says when the knowledge domain shifts you use a new 129 00:11:21,111 --> 00:11:24,740 helper. So I'm going to wish that there was some 130 00:11:24,740 --> 00:11:29,990 other function called larger. That compares the size of these two 131 00:11:29,990 --> 00:11:35,328 images. And produces true if the first image is 132 00:11:35,328 --> 00:11:42,625 larger than remaining images. And if image is larger than the first 133 00:11:42,625 --> 00:11:48,408 thing on the list then what happens? Well that means image is going to go 134 00:11:48,408 --> 00:11:51,670 somewhere after the first thing in the list. 135 00:11:51,670 --> 00:11:58,348 So, what we're going to want to do is say something like, cons the first thing in 136 00:11:58,348 --> 00:12:07,326 the list, i two is bigger than i1. So, i1 the first thing in the list, is 137 00:12:07,326 --> 00:12:15,690 going to come first and then, we'll have the natural recursion. 138 00:12:15,690 --> 00:12:19,100 And the natural recursion will just take image as its first argument. 139 00:12:19,100 --> 00:12:23,650 And what this says is it says, put image somewhere in the rest of the list and put 140 00:12:23,650 --> 00:12:28,250 the current first of the list in front of that. 141 00:12:28,250 --> 00:12:34,165 Otherwise, if image isn't larger than the first thing in the list, then this is 142 00:12:34,165 --> 00:12:39,142 where it goes. So, we are just going to to put image 143 00:12:39,142 --> 00:12:43,990 right here at the front of the list, here it is. 144 00:12:43,990 --> 00:12:51,311 Now, we wish for this function larger, so let's make the wish list entry. 145 00:12:52,460 --> 00:12:55,455 It consumes an image and another image and it produces a Boolean. 146 00:12:55,455 --> 00:13:09,358 Produce true if image one is larger than image two by area. 147 00:13:09,358 --> 00:13:19,224 That's a wish list entry. There's a stub. 148 00:13:19,224 --> 00:13:29,520 Let's get rid of Hydro place now and run and see if we're well formed. 149 00:13:29,520 --> 00:13:32,040 We are well formed, but a bunch of tests are failing. 150 00:13:32,040 --> 00:13:35,802 So what's happened here in this function is we made some name constants in order 151 00:13:35,802 --> 00:13:41,170 to make our tests look better. We just methodically generated all 152 00:13:41,170 --> 00:13:46,374 possible variants for the tests, copied the template as usual, added a parameter. 153 00:13:46,374 --> 00:13:50,028 And then it was all pretty clear what we were going to do, we needed to know the 154 00:13:50,028 --> 00:13:55,148 relationship in size between image and the first thing and the last. 155 00:13:55,148 --> 00:13:59,810 But that involved a knowledge domain shift. 156 00:13:59,810 --> 00:14:03,586 We were switching from the domain of sorting lists to the domain of comparing 157 00:14:03,586 --> 00:14:07,046 the size of images. Those are two different kinds of 158 00:14:07,046 --> 00:14:10,448 knowledge, and so there's a rule that says that's a good place to put a helper 159 00:14:10,448 --> 00:14:14,396 function. So we wished for larger, and that's where 160 00:14:14,396 --> 00:14:20,697 we are right now. Turning again to this overview diagram. 161 00:14:20,697 --> 00:14:24,709 Where we're at is that arrange-images is done but it's waiting for sort-images to 162 00:14:24,709 --> 00:14:27,884 be done. But sort-images is done except its 163 00:14:27,884 --> 00:14:32,930 waiting for insert to be done and insert is waiting for larger to be done. 164 00:14:32,930 --> 00:14:37,650 But so arrange-images, sort-images, and insert are fully coded. 165 00:14:37,650 --> 00:14:41,160 But until larger is done we can't really be sure that those other functions are 166 00:14:41,160 --> 00:14:45,842 done. And larger is what we're going to do in 167 00:14:45,842 --> 00:14:48,050 the next video.