1 00:00:06,410 --> 00:00:08,480 Here's another How To Design Functions problem. 2 00:00:08,480 --> 00:00:12,110 And again, it elaborates on the problem we saw before. 3 00:00:12,110 --> 00:00:16,440 What I'm going to do in this problem is, when I start out, I'm not going to choose 4 00:00:16,440 --> 00:00:20,350 the best signature, and then we'll fix it later. 5 00:00:20,350 --> 00:00:23,480 And the reason I'm doing that is I want to illustrate to you that the how to 6 00:00:23,480 --> 00:00:28,570 design functions recipe is not intended to be what's called a waterfall process. 7 00:00:28,570 --> 00:00:32,040 It's not intended to be something that you do the first step and then the second 8 00:00:32,040 --> 00:00:35,070 step and then the third step and absolutely get each step right. 9 00:00:36,090 --> 00:00:38,530 It happens pretty often. That you go through the recipe and you 10 00:00:38,530 --> 00:00:42,180 get to kind of the third or the fourth step and maybe you realize oh the 11 00:00:42,180 --> 00:00:45,100 signature is not exactly right and you go back and fix it. 12 00:00:46,300 --> 00:00:50,380 And sometimes what even happens is you skip a first step to get to a later step 13 00:00:50,380 --> 00:00:54,400 just to get started. A common example is you're not quite sure 14 00:00:54,400 --> 00:00:57,970 about the signature. So you go write some examples and then 15 00:00:57,970 --> 00:01:02,480 you come back and do the signature. Now what I don't want to do is give you 16 00:01:02,480 --> 00:01:06,590 Carte blanche to jump immediately to the function definition and do that. 17 00:01:06,590 --> 00:01:10,350 That's not going to be what we call systematic design. 18 00:01:10,350 --> 00:01:14,490 But I also don't want you to feel like gee I just don't know what the signature 19 00:01:14,490 --> 00:01:18,150 is but I'm not allowed to do anything else until I write it. 20 00:01:18,150 --> 00:01:21,620 There is some flexibility in following the steps of the process. 21 00:01:21,620 --> 00:01:26,800 It's a structured process but it's not a locked in waterfall process. 22 00:01:26,800 --> 00:01:28,410 So here we go. Let's do this example. 23 00:01:31,240 --> 00:01:34,530 In this problem we're going to design a function called image area that consumes 24 00:01:34,530 --> 00:01:38,880 an image and produces the area of that image. 25 00:01:38,880 --> 00:01:41,850 And it says for area we just need to multiply the image's width by it's 26 00:01:41,850 --> 00:01:45,380 height. So let's see, this function consumes an 27 00:01:45,380 --> 00:01:48,580 image and it produces the area of the image. 28 00:01:50,680 --> 00:01:54,750 Now, you might think this should be number and that's actually going to turn 29 00:01:54,750 --> 00:01:57,640 out not to be actually right, but I'm going to go ahead and pretend that I put 30 00:01:57,640 --> 00:02:01,110 number for now, because one of the things I want to show is that sometimes you 31 00:02:01,110 --> 00:02:05,020 realize, part way through the process, that a piece of what you did before isn't 32 00:02:05,020 --> 00:02:10,118 right. So I'm just going to put number for now. 33 00:02:10,118 --> 00:02:18,010 Then I'm going to say produce image's width times height for area. 34 00:02:18,010 --> 00:02:21,892 Let's see, the stop is going to be define. 35 00:02:21,892 --> 00:02:33,172 Image area what I like to abbreviate image to img and a good dummy value is 36 00:02:33,172 --> 00:02:42,008 zero because its a number and now let's do some examples. 37 00:02:42,008 --> 00:02:51,306 Check expect image area. let's say I have a rectangle that's 2 38 00:02:51,306 --> 00:02:57,040 wide and 3 high and that it's just red. What's the area of that? 39 00:02:57,040 --> 00:03:07,290 Well, the area of that is times 2 3. Alright, let's run that test to see if 40 00:03:07,290 --> 00:03:13,980 it's well formed. oh, rectangle is not defined. 41 00:03:13,980 --> 00:03:17,150 Oh, that's because whenever we use image functions, we need to tell Dr. 42 00:03:17,150 --> 00:03:22,290 Racket that we want to use the image function, and I forgot to do that. 43 00:03:22,290 --> 00:03:25,430 So, we're going to say require 2htvp image. 44 00:03:25,430 --> 00:03:31,000 This is a very common mistake, and I made it here on purpose so that you would see 45 00:03:31,000 --> 00:03:34,510 it happen. You just forget to do the require. 46 00:03:34,510 --> 00:03:39,120 Now, that we've done the require, the test is actually running and failing but, 47 00:03:39,120 --> 00:03:41,660 it's running. So, we're doing reasonably well. 48 00:03:43,790 --> 00:03:46,270 Let's keep going here. Define image area. 49 00:03:46,270 --> 00:03:50,960 So, that's the stub, we'll label it as that. 50 00:03:50,960 --> 00:04:06,930 We'll do the template which will be dot, dot, dot, img. 51 00:04:06,930 --> 00:04:21,340 Now, we'll make a copy of the template. We'll comment out the original template 52 00:04:21,340 --> 00:04:25,890 and label it. And now what's this going to be its gotta 53 00:04:25,890 --> 00:04:30,650 be the images width we're going to use image twice because we need its width and 54 00:04:30,650 --> 00:04:34,750 its height. So, we're going to use image twice. 55 00:04:34,750 --> 00:04:40,820 We're going to say image, width of the image and image height of the image and 56 00:04:40,820 --> 00:04:47,908 we're going to multiply those two together. 57 00:04:47,908 --> 00:04:56,670 Now, let's run that. And the test passed. 58 00:04:56,670 --> 00:05:01,580 But now, we might realize this interesting thing, which is that images 59 00:05:01,580 --> 00:05:08,588 are always sized in pixels, and pixels are, are, are always a natural number, 0, 60 00:05:08,588 --> 00:05:13,350 1, 2, 3, 4, 5, something like that. You can't have 3.2 pixels, that's not how 61 00:05:13,350 --> 00:05:17,260 pixels are. So, if the width is always going to be a 62 00:05:17,260 --> 00:05:21,120 natural, and the height is always going to be a natural, and you multiply 63 00:05:21,120 --> 00:05:25,720 those two, this actually could be natural instead of the number. 64 00:05:25,720 --> 00:05:30,170 And one of the things that we're going to talk about a lot in the course is that 65 00:05:30,170 --> 00:05:35,658 when you write the signature for a function, you always want to use the most 66 00:05:35,658 --> 00:05:40,750 specific types That are correct. Turns out it makes it much easier to 67 00:05:40,750 --> 00:05:45,330 think about using the function and to debug programs if you use the most 68 00:05:45,330 --> 00:05:49,160 specific correct type. So in this case, this function was never 69 00:05:49,160 --> 00:05:53,505 going to produce a floating point number like 3.2. 70 00:05:53,505 --> 00:05:57,220 It's always going to produce a natural. So we take the trouble to make the 71 00:05:57,220 --> 00:06:00,810 signature say that. And I'll save that file and run it one 72 00:06:00,810 --> 00:06:06,094 last time. The test passed.