1 00:00:06,030 --> 00:00:08,310 In this video, I'm going to finish the larger predicate. 2 00:00:09,420 --> 00:00:12,213 And the function itself is mostly straightforward, but there's two 3 00:00:12,213 --> 00:00:17,855 interesting things that come up here. One is an interesting point about writing 4 00:00:17,855 --> 00:00:22,119 tests for functions that consume two of the same type of data. 5 00:00:23,790 --> 00:00:27,572 And the other is the pretty fun point where you've built up a whole chain of 6 00:00:27,572 --> 00:00:33,800 helpers, going from an initial function to a helper to a helper to a helper. 7 00:00:33,800 --> 00:00:37,735 And you finally finish that last helper, and bang, bang, bang, bang, bang. 8 00:00:37,735 --> 00:00:40,550 And if you're lucky the whole thing works. 9 00:00:42,340 --> 00:00:44,421 Okay. Now, I need to finish the larger 10 00:00:44,421 --> 00:00:48,576 predicate and the examples are what I need to do next. 11 00:00:48,576 --> 00:00:51,993 And I'm not going to use the name Constance I defined for the last 12 00:00:51,993 --> 00:00:54,744 function. I'll show you why. 13 00:00:54,744 --> 00:00:57,768 Let me just get started, check-expect larger. 14 00:00:57,768 --> 00:01:03,260 It's really easy in a function like this. Here's function that consumes two images. 15 00:01:03,260 --> 00:01:07,310 And it's going to have to get the width and height of both images. 16 00:01:07,310 --> 00:01:10,910 And it's really easy in a function like this to make a mistake, where, for 17 00:01:10,910 --> 00:01:15,512 example, you used the same image twice. Or you used the width twice instead of 18 00:01:15,512 --> 00:01:19,148 the width and the height. And so what I'm going to do is I'm 19 00:01:19,148 --> 00:01:25,767 going to use numbers for my images. But are very sensitive to making any one 20 00:01:25,767 --> 00:01:30,784 of those mistakes. So watch this, I'll say 3 by 4, solid and 21 00:01:30,784 --> 00:01:35,845 red. Oops, I mean, rectangle, I was thinking 22 00:01:35,845 --> 00:01:42,928 so hard about the numbers, I forgot to make the rectangle. 23 00:01:42,928 --> 00:01:53,140 Rectangle 3, 4, solid, red and rectangle 2, 6, solid, red. 24 00:01:53,140 --> 00:01:56,678 Now the area of both of those is the same, it's twelve, so that should be 25 00:01:56,678 --> 00:02:00,468 false. Now we can make the other four cases 26 00:02:00,468 --> 00:02:05,325 where we'll vary each of the numbers a little bit. 27 00:02:05,325 --> 00:02:12,380 So now, let's make this be 5. Now the first rectangle is bigger. 28 00:02:12,380 --> 00:02:21,010 We'll make this be 5. First rectangle is also bigger. 29 00:02:21,010 --> 00:02:25,012 We'll make Res ?? B5. 30 00:02:25,012 --> 00:02:31,744 Second rectangle is definately bigger, in other words, the first rectangle is 31 00:02:31,744 --> 00:02:38,408 definately not bigger. And for this last one, we'll make six be 32 00:02:38,408 --> 00:02:43,390 seven. The first rectangle is defiuntaely not 33 00:02:43,390 --> 00:02:47,212 bigger. So those are examples run to make sure 34 00:02:47,212 --> 00:02:49,600 it's well formed. It is. 35 00:02:49,600 --> 00:02:52,531 Lots of tests are failing. Now, let's see. 36 00:02:52,531 --> 00:02:58,030 This is a function operating on two primitive atomic data. 37 00:02:58,030 --> 00:03:00,060 So we have to make the template for ourselves. 38 00:03:00,060 --> 00:03:04,850 The template is defined. Larger question mark, img 1. 39 00:03:04,850 --> 00:03:10,493 Img2 and if it was just img2, that would be the template, but we have an 40 00:03:10,493 --> 00:03:16,506 additional atomic parameter, so we add img1. 41 00:03:16,506 --> 00:03:19,854 There we go. Now what do I need to do? 42 00:03:19,854 --> 00:03:24,599 Well, I need to get the area. Of one of them. 43 00:03:24,599 --> 00:03:34,807 So that's the width times the height, and I'll copy here, because I was very 44 00:03:34,807 --> 00:03:44,791 careful about that. So now I've got the top line is the area 45 00:03:44,791 --> 00:03:50,390 of image 1. The bottom line is the area of image 2. 46 00:03:50,390 --> 00:03:56,750 And 1 is bigger than 2 if that's a greater than, and I had to fix the 47 00:03:56,750 --> 00:04:01,551 indentation, run. Wow. 48 00:04:01,551 --> 00:04:13,281 All at once, only one test is failing. Let's go see which test this is. 49 00:04:13,281 --> 00:04:20,320 If I insert I3 into cons I2, I3. Oh, this is just wrong, I made this test 50 00:04:20,320 --> 00:04:23,770 wrong. This was supposed to be the test. 51 00:04:23,770 --> 00:04:30,630 Where I was inserting I3 into a list that already had I1 and I2. 52 00:04:30,630 --> 00:04:34,792 That was just a broken test. If I insert I3 in that list, it should go 53 00:04:34,792 --> 00:04:39,050 at the end. Cons I1, cons I2, cons I3. 54 00:04:39,050 --> 00:04:41,521 Empty is the result. Now let's run it. 55 00:04:41,521 --> 00:04:46,067 All 17 tests passed. So here you're seeing a thing that 56 00:04:46,067 --> 00:04:52,505 happens all the time, when you start designing more complicated functions. 57 00:04:52,505 --> 00:04:55,050 And you have to have more and more helpers. 58 00:04:56,270 --> 00:04:59,620 Is you get to the end, and it kind of all comes together. 59 00:04:59,620 --> 00:05:02,240 And if you're lucky, every single test passes. 60 00:05:02,240 --> 00:05:05,530 In that one final moment every single test now passes. 61 00:05:06,810 --> 00:05:10,973 You're not usually that lucky. You're almost never that lucky. 62 00:05:10,973 --> 00:05:15,173 Usually what happens is you get that final piece done and a bunch of tests 63 00:05:15,173 --> 00:05:19,044 start working. But sometimes what happens is there's 64 00:05:19,044 --> 00:05:22,200 some tests that are broken. Like in this case where there was a 65 00:05:22,200 --> 00:05:26,271 broken test. Other times, what happens is that the 66 00:05:26,271 --> 00:05:28,750 tests are broken. But that some function along the way 67 00:05:28,750 --> 00:05:33,375 actually isn't correct. And then things will only partially work, 68 00:05:33,375 --> 00:05:36,900 or not work at all. This was a pretty good case. 69 00:05:36,900 --> 00:05:39,430 We finished the last function in the chain of helpers. 70 00:05:39,430 --> 00:05:42,350 And we had only one broken test that we had to go fix. 71 00:05:44,410 --> 00:05:48,571 But whether it all works in one fell swoop or not isn't so much the real 72 00:05:48,571 --> 00:05:53,097 issue, as much as that now going back to this picture, what happens is all at 73 00:05:53,097 --> 00:05:57,550 once, all our boxes get checked off because we finished the helper larger 74 00:05:57,550 --> 00:06:06,524 which basically finishes insert. Which basically finishes sort, which 75 00:06:06,524 --> 00:06:10,560 basically finishes arrange-images. Bang, bang, bang, bang, bang. 76 00:06:10,560 --> 00:06:14,446 We check off the whole chain of functions that were waiting on this last helper to 77 00:06:14,446 --> 00:06:17,636 be done. So, from these lectures for the first 78 00:06:17,636 --> 00:06:22,500 part of this week, two main lessons. One is some more helper rules. 79 00:06:22,500 --> 00:06:26,024 The rule about function composition. The rule about operating on arbitrary 80 00:06:26,024 --> 00:06:31,420 size data. The rule about a changing knowledge 81 00:06:31,420 --> 00:06:35,415 domain. The other is this more macro notion that 82 00:06:35,415 --> 00:06:39,575 often when we design more complicated functions, we end up having to design a 83 00:06:39,575 --> 00:06:43,735 bunch of helper functions, as we use different helper rules to split the work 84 00:06:43,735 --> 00:06:50,030 the main of the function out. These rules have an effect Similar to the 85 00:06:50,030 --> 00:06:53,570 effect that the reference rule has, but in this example we've seen some 86 00:06:53,570 --> 00:06:58,147 additional rules. Whenever you're designing functions, for 87 00:06:58,147 --> 00:07:02,115 the rest of this course and in the rest of your life, keep these helper rules in 88 00:07:02,115 --> 00:07:05,298 mind. They're good guidelines for where to put 89 00:07:05,298 --> 00:07:10,558 in a helper function. Some programmers will adopt their own 90 00:07:10,558 --> 00:07:15,516 style for using rules in other places, but these are good general principles for 91 00:07:15,516 --> 00:07:19,333 where to put helper functions.