1 00:00:07,490 --> 00:00:09,860 In the last video, you saw me use the 2 00:00:09,860 --> 00:00:14,110 HtDF Design Recipe to design a very simple function. 3 00:00:14,110 --> 00:00:16,710 But I went through it very quickly, too quickly 4 00:00:16,710 --> 00:00:18,899 for you to absorb the different elements of the recipe. 5 00:00:20,250 --> 00:00:23,480 What I'm going to go through in this video is the same design much 6 00:00:23,480 --> 00:00:27,260 more slowly and talk more carefully about what I'm doing in each step. 7 00:00:28,720 --> 00:00:31,718 What I recommend you do is have your computer open, 8 00:00:31,718 --> 00:00:37,550 open up Dr. Racket and follow along with the design recipe as I'm going through. 9 00:00:37,550 --> 00:00:42,090 And at each step, you can stop the video and catch up in your Dr. Racket. 10 00:00:43,250 --> 00:00:47,730 And I also recommend that you open up a web browser to the HtDF Design 11 00:00:47,730 --> 00:00:50,480 Recipe page from the course website, so that 12 00:00:50,480 --> 00:00:52,360 you can follow along with that as well. 13 00:00:53,780 --> 00:00:57,100 Then you'll be able to see what I'm doing, and practice it as it's going through. 14 00:01:00,600 --> 00:01:02,575 Just like before, I've taken the 15 00:01:02,575 --> 00:01:05,460 double-starter.rkt file from the Week One webpage. 16 00:01:06,680 --> 00:01:08,310 I've opened it up, and here's the problem. 17 00:01:08,310 --> 00:01:11,600 We're supposed to design a function that consumes a number and 18 00:01:11,600 --> 00:01:15,460 produces twice that number and the function should be called double. 19 00:01:15,460 --> 00:01:17,300 Again, its'a very very simple function. 20 00:01:17,300 --> 00:01:20,370 Remember, we're using simple functions to learn the recipe. 21 00:01:20,370 --> 00:01:22,210 And then the recipe will let us do harder functions. 22 00:01:22,210 --> 00:01:25,700 And also, just like in the full 23 00:01:25,700 --> 00:01:28,910 speed version of the video, I've taken a copy of the How 24 00:01:28,910 --> 00:01:33,440 to Design Functions recipe, which I got from the Design Recipes page. 25 00:01:33,440 --> 00:01:37,130 And I'm setting it here on the right-hand side so we can refer to it as we go. 26 00:01:38,880 --> 00:01:41,160 The first step of the recipe is to write the signature. 27 00:01:42,450 --> 00:01:44,820 The job of the signature is to tell me what type 28 00:01:44,820 --> 00:01:48,770 of data a function consumes and what type of data it produces. 29 00:01:50,170 --> 00:01:51,010 In this case, the function 30 00:01:51,010 --> 00:01:53,510 consumes a number and produces a number. 31 00:01:53,510 --> 00:01:58,210 So I write the signature with two semicolons, a space, capitalized number. 32 00:01:58,210 --> 00:02:00,550 Type names are always capitalized. 33 00:02:00,550 --> 00:02:02,400 And then I make a little arrow and 34 00:02:02,400 --> 00:02:05,120 again capitalized number because this function produces a number. 35 00:02:07,310 --> 00:02:09,410 If the function consumed multiple arguments, then 36 00:02:09,410 --> 00:02:12,280 I'd have multiple type names before the arrow. 37 00:02:13,370 --> 00:02:16,270 In this case, it just consumes one argument and I read this 38 00:02:16,270 --> 00:02:20,199 signature as saying, the function consumes a number and produces a number. 39 00:02:25,200 --> 00:02:27,510 Now I need to write the purpose. 40 00:02:27,510 --> 00:02:29,070 The job of the purpose is to give me a 41 00:02:29,070 --> 00:02:35,160 succinct description of what the function produces given what it consumes. 42 00:02:35,160 --> 00:02:37,690 So in this case, a good purpose is to 43 00:02:37,690 --> 00:02:41,520 say that the function produces two times the given number. 44 00:02:41,520 --> 00:02:44,850 Now I know exactly what it's producing in terms of what it consumed. 45 00:02:46,140 --> 00:02:48,400 The purpose needs to say more than the signature. 46 00:02:48,400 --> 00:02:50,650 So purpose, for example, that just says 47 00:02:50,650 --> 00:02:54,160 consumes a number and produces a number isn't telling me any 48 00:02:54,160 --> 00:02:56,570 more than the signature and that wouldn't be a good purpose. 49 00:02:57,670 --> 00:02:59,360 We also want the purpose to be short. 50 00:02:59,360 --> 00:03:03,690 And sometimes its hard to write it short, less than one line, but 51 00:03:03,690 --> 00:03:06,830 it's good to do so because it starts to help you understand the function. 52 00:03:10,040 --> 00:03:13,060 And the stub is like a piece of scaffolding that we're 53 00:03:13,060 --> 00:03:15,900 going to put in place for a short period of time. 54 00:03:15,900 --> 00:03:18,830 It's going to help us with some other parts of our work. 55 00:03:18,830 --> 00:03:20,700 And then we'll end up commenting it out or 56 00:03:20,700 --> 00:03:22,629 in later in the course, we'll just actually delete it. 57 00:03:23,910 --> 00:03:27,860 So it's only lasts a short while but it will do an important piece of work. 58 00:03:29,620 --> 00:03:32,590 What the stub has to be is a function definition 59 00:03:32,590 --> 00:03:35,090 that has the correct function name, in this case double, 60 00:03:36,110 --> 00:03:40,514 has the correct number of parameters, in this case is one, I'll just call it n. 61 00:03:40,514 --> 00:03:43,220 And it produces a dummy result of the correct type. 62 00:03:44,360 --> 00:03:46,930 Since this function produces number, I'll make the 63 00:03:46,930 --> 00:03:49,750 stub produce zero because zero is certainly a number. 64 00:03:51,320 --> 00:03:54,350 So now I'm going to write the examples and tests. 65 00:03:54,350 --> 00:03:58,260 We call them examples and tests, because because they're going to serve both roles. 66 00:04:00,140 --> 00:04:01,600 What I mean by examples 67 00:04:01,600 --> 00:04:05,250 is that often times it's easier to design a general function if 68 00:04:05,250 --> 00:04:09,020 we start with some very specific examples of what it's going to do. 69 00:04:09,020 --> 00:04:09,520 So 70 00:04:11,460 --> 00:04:15,654 in this case, for example, I might write check-expect. 71 00:04:15,654 --> 00:04:22,550 And if I call double with an argument of three, 72 00:04:22,550 --> 00:04:27,420 then what I expect to get back is 6. 73 00:04:27,420 --> 00:04:30,345 And I'll also write check-expect that if I call double 74 00:04:30,345 --> 00:04:36,220 with an argument of 4.2, I'll expect to get back 8.4. 75 00:04:36,220 --> 00:04:41,210 The reason I'm calling double with these two arguments, the reason I have 76 00:04:41,210 --> 00:04:43,270 two examples is I said here in 77 00:04:43,270 --> 00:04:45,800 the signature that the function consumes number. 78 00:04:46,870 --> 00:04:52,350 And by number I mean, real numbers, integers, natural, all kinds of numbers. 79 00:04:52,350 --> 00:04:54,030 And so I'm going to put two examples here just 80 00:04:54,030 --> 00:04:56,380 to really illustrate that I don't just mean integers. 81 00:04:56,380 --> 00:04:59,550 The first example might lead you to think I just mean integers. 82 00:05:00,580 --> 00:05:01,270 We're going to talk a lot 83 00:05:01,270 --> 00:05:03,740 in the course about reasons to have multiple examples 84 00:05:03,740 --> 00:05:07,160 and how many example are enough for a given function. 85 00:05:07,160 --> 00:05:09,170 But here's two examples for this function. 86 00:05:11,480 --> 00:05:14,000 Because I wrapped them in check-expect, they're also 87 00:05:14,000 --> 00:05:16,700 going to be able to serve as tests. 88 00:05:16,700 --> 00:05:18,380 And we'll see shortly how they're going to help 89 00:05:18,380 --> 00:05:20,240 us code the final body of the function. 90 00:05:22,280 --> 00:05:24,160 But first, what we're going to do is make sure 91 00:05:24,160 --> 00:05:27,820 that the example that the check-expects are well formed. 92 00:05:27,820 --> 00:05:29,530 And here's where the stub going to help us. 93 00:05:30,980 --> 00:05:33,778 What I'm going to do now is I'm going to go ahead and run this program. 94 00:05:33,778 --> 00:05:38,086 And when Dr. Racket has a program that has check-expects 95 00:05:38,086 --> 00:05:41,010 in it, what it does is it runs the check-expects. 96 00:05:41,010 --> 00:05:44,410 And it checks to see for each check-expect it, it will 97 00:05:44,410 --> 00:05:47,650 call double with three and it checks to see whether the result 98 00:05:47,650 --> 00:05:48,150 is six. 99 00:05:49,530 --> 00:05:55,350 And then it will call double with 4.2 and checks to see where the result is 8.4. 100 00:05:55,350 --> 00:05:57,560 And if the result isn't what it's supposed to 101 00:05:57,560 --> 00:06:00,610 be, then Dr. Racket reports that the test failed. 102 00:06:02,610 --> 00:06:06,470 Now in this case, both tests failed and I'm very happy. 103 00:06:06,470 --> 00:06:10,320 The reason I'm very happy is both tests actually ran. 104 00:06:10,320 --> 00:06:12,580 Here's what the stub is doing for us. 105 00:06:12,580 --> 00:06:15,549 It's letting us make sure that the tests actually run. 106 00:06:17,250 --> 00:06:20,090 They're going to fail because the stub always produces zero 107 00:06:20,090 --> 00:06:22,200 and that's not the right answer for these cases. 108 00:06:23,290 --> 00:06:25,620 But we're going to know that they ran. 109 00:06:25,620 --> 00:06:28,810 And you'll see later as programs start to get big, that making sure 110 00:06:28,810 --> 00:06:33,540 all your tests are well-formed before you get farther along in the process. 111 00:06:33,540 --> 00:06:37,650 It's a good thing to do because the sooner you find a mistake, the easier it is 112 00:06:37,650 --> 00:06:40,050 to fix. So here we go. 113 00:06:40,050 --> 00:06:43,220 Both of these tests ran and they failed, and I'm really happy. 114 00:06:46,700 --> 00:06:50,309 I want to take a minute here to make an important general point about the recipe. 115 00:06:51,350 --> 00:06:53,700 And that is that every step of the recipe 116 00:06:53,700 --> 00:06:56,360 is intended to help with all the steps after it. 117 00:06:58,030 --> 00:07:01,450 For example, the signature helps us write the purpose, because the signature 118 00:07:01,450 --> 00:07:04,350 tells us that the function consumes a number and produces a number. 119 00:07:07,200 --> 00:07:12,240 Similarly, the signature helps us write the stub, because the signature says the 120 00:07:12,240 --> 00:07:17,910 function consumes a single argument so this function has one parameter. 121 00:07:17,910 --> 00:07:20,280 It's a number, that's why I called it n. 122 00:07:20,280 --> 00:07:23,530 It also tells us that the function produces a number, that's why 123 00:07:23,530 --> 00:07:27,380 I chose zero as the dummy value for the stub to produce. 124 00:07:29,020 --> 00:07:32,190 The signature also helps us write the check-expect. 125 00:07:32,190 --> 00:07:36,340 It tells us that this function, when I sit there and write check-expects double and 126 00:07:36,340 --> 00:07:40,390 then I ask myself what to put, well, the signature tells me put a number. 127 00:07:42,120 --> 00:07:43,120 And then when I try to write the 128 00:07:43,120 --> 00:07:46,700 expected value, the signature says that it's a number 129 00:07:46,700 --> 00:07:48,780 and the purpose tells me exactly how that 130 00:07:48,780 --> 00:07:54,140 number relates to the argument in the example call. 131 00:07:55,820 --> 00:07:57,260 The key thing is when 132 00:07:57,260 --> 00:08:00,330 you're trying to figure out what to write at one step of the 133 00:08:00,330 --> 00:08:03,450 recipe, look at what you wrote at the previous steps of the recipe. 134 00:08:03,450 --> 00:08:06,450 That's how the recipe is helping you is it's letting you 135 00:08:06,450 --> 00:08:10,310 slowly build up the knowledge you need to design the final function. 136 00:08:12,110 --> 00:08:15,270 Next up in the recipe is the template or sometimes called the inventory. 137 00:08:17,300 --> 00:08:21,040 Starting next week, we're going to get richer and richer templates. 138 00:08:21,040 --> 00:08:23,960 But for this week, the template is going to be quite simple. 139 00:08:23,960 --> 00:08:27,615 The template is a function with the right function name and the right parameter. 140 00:08:27,615 --> 00:08:30,280 Ad for this week, the body of the template 141 00:08:30,280 --> 00:08:33,670 is just going to be open paran dot dot dot n. 142 00:08:34,710 --> 00:08:36,800 And the way we're going to read that, is we're going to read 143 00:08:36,800 --> 00:08:42,480 that as saying, hey, the outline of this function is that it's 144 00:08:42,480 --> 00:08:46,400 going to do something, that's what the dots mean, it's do something. 145 00:08:46,400 --> 00:08:49,008 It's going to do something with parameter n. 146 00:08:49,008 --> 00:08:53,080 Now, it's the role of the template, is to give us kind of outline of the function. 147 00:08:55,980 --> 00:08:57,928 What I'm going to do here is label the template 148 00:08:57,928 --> 00:09:02,170 and now we're going to make a copy of it 149 00:09:03,420 --> 00:09:05,960 I'll put the copy here, I'll remove the label 150 00:09:05,960 --> 00:09:09,590 from the copy and I'll common out the original template. 151 00:09:11,150 --> 00:09:12,530 What you're going to do later in the course is 152 00:09:12,530 --> 00:09:15,622 you're not actually going to keep a copy of the template. 153 00:09:15,622 --> 00:09:18,270 What we've found for the first few weeks of the course that 154 00:09:18,270 --> 00:09:21,620 it helps to keep a copy of the stub and template around and 155 00:09:21,620 --> 00:09:26,220 that's why that's what I am doing here. There's the template. 156 00:09:26,220 --> 00:09:28,820 It's the outline of the final function definition. 157 00:09:30,740 --> 00:09:33,790 Now, I'm going to code the function body In this step, I'm going to use 158 00:09:33,790 --> 00:09:37,610 everything I've written before to let me know how to finish the function body. 159 00:09:39,670 --> 00:09:43,670 One thing that's useful to do sometimes is elaborate the examples. 160 00:09:43,670 --> 00:09:47,943 So in this example, I know the double of 4.2 is 8.4, but what 161 00:09:47,943 --> 00:09:51,960 I'm going to do now is make it more clear to me why that's true. 162 00:09:51,960 --> 00:09:55,240 The reason that's true is that it's times 2 of 4.2. 163 00:09:55,240 --> 00:10:00,440 And now all at once, I know exactly how to finish this function body. 164 00:10:00,440 --> 00:10:03,440 It's just times 2 of n, whatever n is. 165 00:10:08,430 --> 00:10:10,130 The last step is to run the test. 166 00:10:11,590 --> 00:10:16,270 So I run the test here and I get both tests pass, which makes me pretty happy. 167 00:10:17,420 --> 00:10:20,309 We'll see examples later about what to do when the tests don't pass. 168 00:10:21,740 --> 00:10:24,580 Now you've seen the HtDF Design Recipe 169 00:10:24,580 --> 00:10:27,020 used twice to design the same simple function. 170 00:10:28,410 --> 00:10:31,350 In the last video, I went through it quite quickly. 171 00:10:31,350 --> 00:10:33,620 And in this video, I went through it in slow motion, 172 00:10:33,620 --> 00:10:35,560 where I talked in detail about each step. 173 00:10:37,510 --> 00:10:39,630 At this point, you should be starting to understand 174 00:10:39,630 --> 00:10:42,250 what to do at each step of the design recipe. 175 00:10:44,020 --> 00:10:47,410 If you feel comfortable with that then I would suggest you go ahead 176 00:10:47,410 --> 00:10:52,260 to the next video in which we will work another HtDF design problem together. 177 00:10:53,910 --> 00:10:56,820 If you don't necessarily feel comfortable with it then 178 00:10:56,820 --> 00:10:58,780 I would suggest that you take a Blank Editor 179 00:10:58,780 --> 00:11:02,660 tab and rework the process of designing the 180 00:11:02,660 --> 00:11:07,480 function from this video on your own, step-by-step. 181 00:11:07,480 --> 00:11:09,790 But when you do it, do be sure to have 182 00:11:09,790 --> 00:11:14,010 the HtDF Design Recipe page from the web site open. 183 00:11:14,010 --> 00:11:18,720 Use that as a reference whenever you're using the HtDF Design Recipe. 184 00:11:18,720 --> 00:11:21,750 Our goal is not for you to memorize the recipe. 185 00:11:21,750 --> 00:11:24,890 Our goal is for you to learn how to use it as a resource 186 00:11:24,890 --> 00:11:27,510 in designing increasingly more complicated functions.