1 00:00:07,880 --> 00:00:12,010 In this video, I want to introduce one of the most important elements of the course. 2 00:00:12,010 --> 00:00:17,669 The How To Design Functions recipe, where we call it the HTDF Recipe for short. 3 00:00:19,310 --> 00:00:22,900 The HTDF Recipe systematizes the design of a function. 4 00:00:24,040 --> 00:00:26,534 What I mean by that, is it tells us what to do first, 5 00:00:26,534 --> 00:00:29,980 and second, and third, all the way through the design of a function. 6 00:00:31,440 --> 00:00:33,260 So it helps us know what to do, and 7 00:00:33,260 --> 00:00:37,565 it also helps us be sure that we end up with a high quality function. 8 00:00:37,565 --> 00:00:43,020 That really does what we need it to do, and that's been well-tested. 9 00:00:43,020 --> 00:00:45,620 Now, there's a funny thing about design recipes or design 10 00:00:45,620 --> 00:00:49,510 methods, as they're sometimes called, particularly effects when you learn them. 11 00:00:51,240 --> 00:00:54,970 What a design recipe does, is it makes hard problems easier. 12 00:00:54,970 --> 00:00:58,561 It lets us take a big, hard problem and break it down and work through 13 00:00:58,561 --> 00:01:00,150 it in a systematic fashion. 14 00:01:02,280 --> 00:01:03,512 The price we pay for that is 15 00:01:03,512 --> 00:01:06,150 that it actually makes easy problems more cumbersome. 16 00:01:07,280 --> 00:01:10,748 That's right, the HTDF Recipe is going to make hard functions 17 00:01:10,748 --> 00:01:14,510 easier to design, and easy functions a little harder to design. 18 00:01:15,650 --> 00:01:18,994 In fact, once you get good at designing functions, 19 00:01:18,994 --> 00:01:22,940 you won't use the HDTF recipe for really simple functions. 20 00:01:24,380 --> 00:01:27,488 But for now, we want you to use the HDTF recipe even 21 00:01:27,488 --> 00:01:29,960 for simple functions. 22 00:01:29,960 --> 00:01:32,222 That's because the way this is going to work, is 23 00:01:32,222 --> 00:01:35,310 that you're going to use simple functions to learn the recipe. 24 00:01:36,390 --> 00:01:39,890 And then you're going to use the recipe to do the hard functions. 25 00:01:39,890 --> 00:01:42,439 So what I need to ask for now is two kinds of patience. 26 00:01:43,590 --> 00:01:46,440 If you've never programmed before, it's going to seem like 27 00:01:46,440 --> 00:01:49,202 there's a lot going on, when we introduce this recipe. 28 00:01:49,202 --> 00:01:52,658 So, I'm going to need to ask, that you be patient, and go through 29 00:01:52,658 --> 00:01:54,450 it with me at the pace we're going 30 00:01:54,450 --> 00:01:57,450 through, and I promise you it will become familiar. 31 00:01:59,640 --> 00:02:03,070 If you have programmed before, this is going to seem like overkill. 32 00:02:03,070 --> 00:02:05,986 We're going to start with a very simple function, and by using the 33 00:02:05,986 --> 00:02:08,400 recipe it's going to take us quite a long time to do it. 34 00:02:08,400 --> 00:02:12,560 I need to ask you to be patient too. 35 00:02:12,560 --> 00:02:15,508 I promise you within a few weeks by using the recipe, 36 00:02:15,508 --> 00:02:20,010 we're going to be designing much harder functions than we can design otherwise. 37 00:02:21,310 --> 00:02:23,670 Here's how we're going to go through this material. 38 00:02:23,670 --> 00:02:24,859 There's going to be this video and 39 00:02:24,859 --> 00:02:25,390 the next one. 40 00:02:26,860 --> 00:02:30,394 In the first video, I'm going to go through the design of a function 41 00:02:30,394 --> 00:02:34,840 step by step, and I'm going to tell you what I'm doing at each step. 42 00:02:34,840 --> 00:02:36,730 But I'm going to describe it, as if 43 00:02:36,730 --> 00:02:39,700 you already knew the terminology of the design recipe. 44 00:02:41,070 --> 00:02:43,268 I know that sounds silly because you don't. 45 00:02:43,268 --> 00:02:47,272 But we've learned from teaching this to many, many students 46 00:02:47,272 --> 00:02:49,967 at UBC, that the right thing to do first is 47 00:02:49,967 --> 00:02:53,072 to kind of go through it at almost normal speed. 48 00:02:53,072 --> 00:02:56,426 Then in the next video, the next video is going to 49 00:02:56,426 --> 00:02:59,833 kind of be a slow motion version of this video. 50 00:02:59,833 --> 00:03:04,813 In that next video, as we go through it piece by piece, I'll elaborate 51 00:03:04,813 --> 00:03:09,493 on each piece we're doing and why we're doing it and how it works. 52 00:03:09,493 --> 00:03:15,583 So what we're going to ask you to do is watch this video once at speed, 53 00:03:15,583 --> 00:03:21,370 then watch the next video with Dr. Racket open following along. 54 00:03:21,370 --> 00:03:24,870 And then it'll probably help you to watch this video again at speed. 55 00:03:26,340 --> 00:03:29,490 Everything we do, the entire design recipe, you can 56 00:03:29,490 --> 00:03:33,090 find on the course webpage, under the Design Recipes link. 57 00:03:34,390 --> 00:03:37,288 That example, that's used on the website, is the one 58 00:03:37,288 --> 00:03:41,350 we're going to do here, so you'll be able to follow along. 59 00:03:41,350 --> 00:03:45,770 Again, be patient with the material, and be patient with yourself. 60 00:03:45,770 --> 00:03:48,800 There's a lot going on in the HTDF Recipe. 61 00:03:48,800 --> 00:03:51,420 You're not going to pick it up the first time through. 62 00:03:51,420 --> 00:03:54,520 You're not going to pick it up the second time through. 63 00:03:54,520 --> 00:03:58,390 But you'll be going through the recipe hundreds of times through the course. 64 00:03:58,390 --> 00:04:02,150 And I promise you, within a couple weeks, it's going to seem quite familiar. 65 00:04:02,150 --> 00:04:04,929 And you'll just be learning more nuances about it as we go forward. 66 00:04:07,420 --> 00:04:11,155 Okay, what I've done here is I've opened the double 67 00:04:11,155 --> 00:04:15,034 starter Dr Racket file that I got from the one webpage. 68 00:04:17,482 --> 00:04:20,450 I've opened it and I'm getting it tells me the problem we need to do. 69 00:04:20,450 --> 00:04:24,470 We need to design a function that consumes a number that produces twice that number. 70 00:04:24,470 --> 00:04:27,656 We're going to call the function double, and we need to follow the 71 00:04:27,656 --> 00:04:32,040 How To Design Funstion recipe, and show the stub and the template. 72 00:04:32,040 --> 00:04:33,832 And remember, I'm doing this at full 73 00:04:33,832 --> 00:04:36,780 speed, I'm not explaining every step of it. 74 00:04:36,780 --> 00:04:39,230 This video will take just a couple of minutes and then in the 75 00:04:39,230 --> 00:04:42,130 next video, we'll do it slower and I'll explain every bit of it. 76 00:04:43,990 --> 00:04:46,640 But whenever I follow the How To Design Functions recipe, I 77 00:04:46,640 --> 00:04:49,060 like to have an overview of the recipe in front of me. 78 00:04:50,450 --> 00:04:53,410 So what I've done is to go to the first website. 79 00:04:54,510 --> 00:04:56,900 And I went to the Design Recipes overview tab. 80 00:04:58,130 --> 00:05:00,062 And over here on the side is a 81 00:05:00,062 --> 00:05:03,980 sub, overview of the How To Design Functions recipe. 82 00:05:05,100 --> 00:05:09,068 And I've taken that and copied it in my video editor and set it right here, 83 00:05:09,068 --> 00:05:11,840 so that you could see it while I work. 84 00:05:11,840 --> 00:05:16,130 Okay, so step one is signature, purpose, and stub. 85 00:05:16,130 --> 00:05:19,364 So, this function is going to consume a number and 86 00:05:19,364 --> 00:05:23,240 it's going to produce a number, so that's my signature. 87 00:05:24,610 --> 00:05:31,840 It's purpose is going to be to produce two times the given number. 88 00:05:33,770 --> 00:05:34,460 Okay, 89 00:05:34,460 --> 00:05:41,590 and the stop, let's see, this function is called double and it consumes 90 00:05:41,590 --> 00:05:46,660 a single argument which is number. I'll call it n. 91 00:05:46,660 --> 00:05:50,040 And it produces a number, so for now we'll have it produce zero. 92 00:05:50,040 --> 00:05:51,220 And this is the stub. 93 00:05:54,070 --> 00:05:54,570 Okay? 94 00:05:57,460 --> 00:05:59,605 Now the next step I've gotta do is the examples 95 00:05:59,605 --> 00:06:03,019 wrapped in check-expect, and those are going to go right here actually. 96 00:06:04,350 --> 00:06:09,230 What I'm going to say is I'm going to say check-expect. 97 00:06:09,230 --> 00:06:14,516 Expect that if I call double with 3, I'll get 6. 98 00:06:16,550 --> 00:06:21,915 And I'll give another one, check-expect 99 00:06:21,915 --> 00:06:27,135 that if I call double, with 4.2, I'll 100 00:06:27,135 --> 00:06:32,131 get 8.4 like that. And then I'm going to run 101 00:06:32,131 --> 00:06:36,856 my check-expects and let's see, they're failing 102 00:06:36,856 --> 00:06:41,652 but both of them are running. So I'm quite happy 103 00:06:41,652 --> 00:06:42,750 they're both running. 104 00:06:46,100 --> 00:06:48,468 Now I'll come back up here and the stub 105 00:06:48,468 --> 00:06:51,690 has done its job, it let me run my check-expects. 106 00:06:51,690 --> 00:06:57,276 So I'm going to commend out the stub, and I'll go on the next step of 107 00:06:57,276 --> 00:07:03,110 the recipe is to write the templates, so I'll say define double of n. 108 00:07:04,110 --> 00:07:08,190 And the template for this function is dot dot dot n. 109 00:07:11,170 --> 00:07:12,210 That's the template. 110 00:07:18,420 --> 00:07:22,619 And actually, what I'm going to do is make a copy of the template. 111 00:07:28,280 --> 00:07:31,430 And I won't mark the template, the copy saying it is the copy. 112 00:07:31,430 --> 00:07:34,330 I'm going to copy out, comment out the actual template. 113 00:07:37,740 --> 00:07:41,400 The next to the last step of the recipe is to code the function body. 114 00:07:41,400 --> 00:07:43,678 So it might be useful to me at this point to 115 00:07:43,678 --> 00:07:46,930 be a little bit more clear for myself why that's 8.4. 116 00:07:46,930 --> 00:07:52,119 The reason that's 8.4 is that it's really times 2 of 4.2. 117 00:07:56,410 --> 00:07:58,740 Oh, and now I understand how this function works. 118 00:07:58,740 --> 00:08:02,162 It just multiplies whatever n is, 119 00:08:02,162 --> 00:08:02,752 [SOUND] 120 00:08:02,752 --> 00:08:10,360 by 2. Now I'll run it, and both tests pass. 121 00:08:13,320 --> 00:08:16,600 So that's a first pass through the HTDF Design Recipe. 122 00:08:17,710 --> 00:08:20,591 Again, I certainly don't expect you to have fully 123 00:08:20,591 --> 00:08:23,900 absorbed the HTDF Recipe from that quick pass through it. 124 00:08:25,090 --> 00:08:28,350 What I want you to do now is go to the next video. 125 00:08:28,350 --> 00:08:31,192 Which is going to be the slow motion version of this video, in 126 00:08:31,192 --> 00:08:34,790 which I'll talk about the different parts of the recipe in more detail.