1 00:00:06,050 --> 00:00:09,118 In this last video on the bar chart problem, we're going to start by coding 2 00:00:09,118 --> 00:00:14,552 the body of the chart function. We're very quickly going to run into the 3 00:00:14,552 --> 00:00:17,684 natural helper in the template, and that's going to force us to split the 4 00:00:17,684 --> 00:00:23,326 chart function into two functions. But don't worry, we're not going to lose 5 00:00:23,326 --> 00:00:27,440 all that work we did on the examples, okay? 6 00:00:27,440 --> 00:00:29,010 So, let's see. I've got the signature. 7 00:00:29,010 --> 00:00:31,860 I've got the purpose. I've got the sub, I've got the examples. 8 00:00:31,860 --> 00:00:36,012 It's time to write function, the actual function definition. 9 00:00:36,012 --> 00:00:41,185 So, let me go get the template, because this function consumes list of school. 10 00:00:41,185 --> 00:00:53,570 I'll comment out the stub. Now, let's see. 11 00:00:53,570 --> 00:00:58,274 I copy down the template. I rename the function, and I rename the 12 00:00:58,274 --> 00:01:01,940 natural recursion, okay? I'll start with the base case. 13 00:01:01,940 --> 00:01:04,430 Well, what happens in the base case? What happens when the list is empty? 14 00:01:04,430 --> 00:01:07,210 Well, what happens when the list is empty is just this thing right here. 15 00:01:07,210 --> 00:01:11,972 It's just this. In fact, you're allowed to copy from the 16 00:01:11,972 --> 00:01:20,099 examples for the function, not the other way around. 17 00:01:20,099 --> 00:01:22,908 So, we'll put that right there so, we don't type it. 18 00:01:22,908 --> 00:01:25,880 Now, remember the thing I showed you earlier this week. 19 00:01:25,880 --> 00:01:29,240 I can actually test this now. And tests are going to fail. 20 00:01:29,240 --> 00:01:32,844 Tests are going to fail. But the first test that failed is 21 00:01:32,844 --> 00:01:39,790 actually the second one. The base case test has now run properly. 22 00:01:39,790 --> 00:01:42,247 It's not too surprising, because the base case of this function wasn't very 23 00:01:42,247 --> 00:01:46,247 complicated. But the base case is running. 24 00:01:46,247 --> 00:01:50,850 Now, what do we do here? Well, let's look at this. 25 00:01:50,850 --> 00:01:54,756 For a case where there's a school where there is one school or maybe, and then 26 00:01:54,756 --> 00:01:58,656 some rest of the list. Here's a case where there's one school 27 00:01:58,656 --> 00:02:02,164 and some rest of the list. Let me change the line breaking, so, it's 28 00:02:02,164 --> 00:02:06,316 like that. We do beside/align bottom of some 29 00:02:06,316 --> 00:02:13,148 complicated thing having to do with the school, and then the result of the 30 00:02:13,148 --> 00:02:21,190 natural recursion, okay? Well, besides/align bottom seems like the 31 00:02:21,190 --> 00:02:31,577 combination. So, that's the combination, that's the 32 00:02:31,577 --> 00:02:40,629 natural recursion. So, this whole complicated thing here. 33 00:02:41,740 --> 00:02:43,550 We've got to get that in here. Now, watch this. 34 00:02:43,550 --> 00:02:49,540 This is the consequence of the reference rule. 35 00:02:49,540 --> 00:02:53,520 This is what it means for something to be a natural helper. 36 00:02:53,520 --> 00:02:57,900 When you're working on a function like this and you see that you have to do 37 00:02:57,900 --> 00:03:04,429 something complicated with the school. You have to do something complicated with 38 00:03:04,429 --> 00:03:09,670 the referred to type. Remember los refers to school, list of 39 00:03:09,670 --> 00:03:14,350 school refers to school. And here I have to do something 40 00:03:14,350 --> 00:03:19,966 complicated with the school. When you've got this natural helper in 41 00:03:19,966 --> 00:03:23,200 the template, it's telling you don't do it here. 42 00:03:23,200 --> 00:03:25,985 Do it elsewhere, make somebody else do it. 43 00:03:25,985 --> 00:03:31,770 Punt, wait. What it's telling you is make a wish. 44 00:03:31,770 --> 00:03:36,270 So, instead of trying to do the old school thing here, I'm just going to make 45 00:03:36,270 --> 00:03:39,760 a wish. I'm going to say I wish there was a 46 00:03:39,760 --> 00:03:45,910 function called make-bar that concerned the school and produced a single bar. 47 00:03:45,910 --> 00:03:49,501 I just wished that function existed. And just as we have before, whenever we 48 00:03:49,501 --> 00:03:54,240 wish for a function, right now is when I write the wish list entry. 49 00:03:54,240 --> 00:04:00,750 So, let's see, I'm handing it a school because first los produces a school. 50 00:04:00,750 --> 00:04:04,000 So, it gets a school and has to produce an image. 51 00:04:04,000 --> 00:04:11,480 It has to produce the bar for a single school in the bar chart. 52 00:04:11,480 --> 00:04:16,590 And the stub oh, it's, it's a wish list, so we'll put bang bang bang. 53 00:04:16,590 --> 00:04:26,560 And the stub is defined, make bar s. And then the stub for a function that 54 00:04:26,560 --> 00:04:31,040 produces an image, we'll say square 0, solid, white. 55 00:04:31,040 --> 00:04:41,150 And now I've wished for it, and I can run the test for chart right now. 56 00:04:41,150 --> 00:04:44,635 Now, they're going to fail. Okay, they're going to fail, because the 57 00:04:44,635 --> 00:04:49,783 actual values are this. The actual values have nothing in them, 58 00:04:49,783 --> 00:04:53,786 because make bar keeps making these blank images. 59 00:04:53,786 --> 00:04:58,394 But in some very real sense now chart is done, or at least chart looks like it's 60 00:04:58,394 --> 00:05:01,835 done. If only we had make bar, then chart would 61 00:05:01,835 --> 00:05:05,100 probably run, unless there's some mistakes in it. 62 00:05:06,760 --> 00:05:11,936 What happened here is I wished for what's called a helper function. 63 00:05:11,936 --> 00:05:16,402 And the reason I did that goes back to that picture we saw about the type 64 00:05:16,402 --> 00:05:23,740 comments of these types in the templates. Right here in this type comment, there's 65 00:05:23,740 --> 00:05:27,710 a reference. And a reference produces a natural helper 66 00:05:27,710 --> 00:05:31,551 in the template. And what that meant was, when we got 67 00:05:31,551 --> 00:05:34,010 here. I'll back it up a second. 68 00:05:34,010 --> 00:05:39,302 We got here, and we were thinking about doing this complicated stuff for the 69 00:05:39,302 --> 00:05:45,875 individual school, but the template was saying, fun for school. 70 00:05:45,875 --> 00:05:49,130 And what fun for school says to us is it says, no, no, no. 71 00:05:49,130 --> 00:05:53,470 If what you have to do with school is complicated, don't do it here. 72 00:05:54,590 --> 00:05:58,480 Wish for it, and that's why we decided to wish for it. 73 00:05:58,480 --> 00:06:02,860 Now, what's the actual rule for complicated? 74 00:06:02,860 --> 00:06:08,080 You should go check the recipe pages for the reference rule for this. 75 00:06:08,080 --> 00:06:13,008 But the basic rule is, if you have to call more than one function that operates 76 00:06:13,008 --> 00:06:18,010 on school, like a helper. So, if it turned out that all you needed 77 00:06:18,010 --> 00:06:21,310 to do here for example, was call a selector on school, we wouldn't need to 78 00:06:21,310 --> 00:06:25,210 make a new function. If it turns out that new function you 79 00:06:25,210 --> 00:06:28,420 need already exist, then you don't need to make another one. 80 00:06:28,420 --> 00:06:31,177 You can call a function that already exists. 81 00:06:31,177 --> 00:06:35,145 But the idea here is that whenever I see fun for school in the template, I want to 82 00:06:35,145 --> 00:06:39,260 see a single call to a function consumes school. 83 00:06:41,350 --> 00:06:43,240 Let's just finish this up now, we're almost done. 84 00:06:43,240 --> 00:06:48,425 Let's see make-bar, check-expect, make-bar. 85 00:06:48,425 --> 00:06:55,033 Now, all this work I did up here to figure out exactly what I want to see for 86 00:06:55,033 --> 00:07:05,387 an individual school. I'm allowed to reuse that work now, okay? 87 00:07:05,387 --> 00:07:15,390 I'm allowed to do this, and say, I know what I want that to look like. 88 00:07:15,390 --> 00:07:21,072 I figured it out before, I want it to look like this. 89 00:07:21,072 --> 00:07:36,440 Well, there's a single check expect, I only need one. 90 00:07:36,440 --> 00:07:49,514 Let me go get the template for school. I'll comment out the stub. 91 00:07:49,514 --> 00:08:04,448 I'll rename this function, and I gotta do this whole complicated thing. 92 00:08:04,448 --> 00:08:12,942 Overlay, align of center bottom and then rotate 90, and then the text of the 93 00:08:12,942 --> 00:08:21,104 school name. There's where the school name comes in 94 00:08:21,104 --> 00:08:29,960 for S1. Font size, font color. 95 00:08:32,160 --> 00:08:35,155 That's the name, and then let's see what else do we got? 96 00:08:35,155 --> 00:08:40,160 We've got a rectangle. Whoops, not capital rectangle. 97 00:08:40,160 --> 00:08:53,024 Rectangle of the bar width and times the tuition, whatever the tuition is the 98 00:08:53,024 --> 00:09:03,930 y-scale. And that rectangle is outline black. 99 00:09:03,930 --> 00:09:12,939 And there's another rectangle which is the same size, but it's solid and bar 100 00:09:12,939 --> 00:09:17,592 color. Let's try that. 101 00:09:17,592 --> 00:09:21,880 All four tests passed. All four tests passed. 102 00:09:21,880 --> 00:09:27,840 Let me show it to you down here, just so you see, you really see it. 103 00:09:27,840 --> 00:09:30,800 If I say chart of LOS1, I get blank because that's the base case. 104 00:09:30,800 --> 00:09:39,225 But if I say chart of LOS2, I get that little chart. 105 00:09:40,740 --> 00:09:44,290 So, two things about this example, really important to see. 106 00:09:44,290 --> 00:09:48,920 One we've seen before, but it's always worth remembering. 107 00:09:48,920 --> 00:09:51,680 Which is, when we were doing the examples, it might have seem to you, when 108 00:09:51,680 --> 00:09:54,450 are we going to get there? When are we going to get there? 109 00:09:54,450 --> 00:09:57,782 When are we going to get there? But notice how at the end it all happened 110 00:09:57,782 --> 00:10:00,860 really fast. That's because we spent so much time 111 00:10:00,860 --> 00:10:04,670 figuring it out what we really, really wanted when we were doing the examples. 112 00:10:04,670 --> 00:10:07,930 So, that's an old point, we've seen that point before. 113 00:10:07,930 --> 00:10:12,070 The new point has to do with the reference relationship in these types. 114 00:10:12,070 --> 00:10:15,654 Because these types have a reference relationship in them, the template for 115 00:10:15,654 --> 00:10:18,721 list of school has a natural helper in it. 116 00:10:18,721 --> 00:10:22,379 And what that meant was that we were sitting right here trying to figure out 117 00:10:22,379 --> 00:10:26,567 how to do this complicated thing with a single school. 118 00:10:26,567 --> 00:10:30,340 The template was staring us in the face saying, fun for school. 119 00:10:30,340 --> 00:10:34,970 Which means, don't do the complicated thing here, do it in a helper. 120 00:10:34,970 --> 00:10:38,795 So, we wished for the helper, then we designed the helper and we were all done. 121 00:10:38,795 --> 00:10:42,323 What we've seen in this example is something that's really critical to good 122 00:10:42,323 --> 00:10:45,601 software design. Which is that the structure of the 123 00:10:45,601 --> 00:10:49,241 information flows all the way through to the structure of the functions in the 124 00:10:49,241 --> 00:10:54,597 program. What we saw at the beginning, was that we 125 00:10:54,597 --> 00:10:59,496 decided the structure of the information was naturally information about schools, 126 00:10:59,496 --> 00:11:05,100 and information about an arbitrary number of those schools. 127 00:11:05,100 --> 00:11:09,710 And what that did was it put a reference relationship in the type comment. 128 00:11:09,710 --> 00:11:13,935 And that reference relationship in the type comment led to a natural helper in 129 00:11:13,935 --> 00:11:19,720 the templates, which led to a helper function call in the actual functions. 130 00:11:20,860 --> 00:11:24,768 So, it's really key here. Designing data is actually making 131 00:11:24,768 --> 00:11:29,000 important decisions about the design of functions. 132 00:11:29,000 --> 00:11:31,255 And we're going to see that again and again in the next few weeks of the 133 00:11:31,255 --> 00:11:32,180 course.