1 00:00:06,160 --> 00:00:09,820 The next three videos are examples of designing a function that consumes 2 00:00:09,820 --> 00:00:14,736 non-primitive data, data that's defined by your own data definition. 3 00:00:14,736 --> 00:00:21,640 Now, they are HtDF problems so mostly you know how to do them. 4 00:00:21,640 --> 00:00:26,340 Remember, I said the HGDF recipe is largely orthogonal to the form of data. 5 00:00:27,380 --> 00:00:30,530 But it isn't completely orthogonal [INAUDIBLE] form of data. 6 00:00:30,530 --> 00:00:33,692 One real dependence between function design and form of data has to do with 7 00:00:33,692 --> 00:00:38,220 the number of tasks that's appropriate for that function. 8 00:00:38,220 --> 00:00:41,236 So in these videos I'm going to pay particular attention to the number of 9 00:00:41,236 --> 00:00:44,695 test that we're going to use based on the form of data. 10 00:00:44,695 --> 00:00:50,213 >> I mean aisle-starter.rkt and the problem we're asked is to use the SeatNum 11 00:00:50,213 --> 00:00:54,890 data definition. And design a function that produces true 12 00:00:54,890 --> 00:00:58,864 if the given seat number is on the isle. So the first thing I would do is go look 13 00:00:58,864 --> 00:01:03,239 at the SeatNum data definition. Then it reminds me that seat number is a 14 00:01:03,239 --> 00:01:08,090 natural number from 1 to 32 inclusive, and that its seat numbers in a row and 1 15 00:01:08,090 --> 00:01:13,820 and 32 are the aisle seats. So now I can get started on my function. 16 00:01:13,820 --> 00:01:16,759 I'll quickly do the signature purpose and stub. 17 00:01:16,759 --> 00:01:20,471 Let's see, it's going to consume a SeatNum and we're supposed to produce 18 00:01:20,471 --> 00:01:23,983 true when the seat number is on the aisle. 19 00:01:23,983 --> 00:01:26,950 So that means it's going to produce a Boolean. 20 00:01:26,950 --> 00:01:30,540 And it'll produce false when the seat number isn't on the aisle. 21 00:01:30,540 --> 00:01:36,340 [SOUND] And let's see, produce true if given seat number is on the aisle. 22 00:01:36,340 --> 00:01:48,453 [SOUND] And, let's see the function. This is a predicate, so we'll name it 23 00:01:48,453 --> 00:01:52,907 using the naming convention for predicates like this, (aisle? 24 00:01:52,907 --> 00:01:59,339 sn) is and we'll just produce false for the stub. 25 00:01:59,339 --> 00:02:02,799 So let me run that to make sure the stub is well formed than it is. 26 00:02:02,799 --> 00:02:05,237 Okay. Now let's get to the examples. 27 00:02:05,237 --> 00:02:07,499 Now here is the first place where the data definition is really go have 28 00:02:07,499 --> 00:02:10,663 something to say. If we go back to the How to Design Data 29 00:02:10,663 --> 00:02:14,311 page, and we go to the section on Intervals, and we go to the section on 30 00:02:14,311 --> 00:02:18,663 Guidance on Examples and Tests, it tells us that when writing tests for functions 31 00:02:18,663 --> 00:02:23,015 operating on intervals, we should be sure to test closed boundaries as well as mid 32 00:02:23,015 --> 00:02:29,470 points. And always be sure to include enough 33 00:02:29,470 --> 00:02:32,630 tests to check all other points of variance. 34 00:02:32,630 --> 00:02:35,770 So let's go back to the code. I've got an interval with two closed 35 00:02:35,770 --> 00:02:39,720 endpoints. So I know that what I'm going to need are 36 00:02:39,720 --> 00:02:46,145 three tests. One [SOUND] for the first closed end 37 00:02:46,145 --> 00:02:51,480 point which is 1. 1 for the middle, which is, you know, 38 00:02:51,480 --> 00:02:54,823 middle doesn't have to be the exact middle. 39 00:02:54,823 --> 00:03:00,803 Something like the middle. Well call it 16 and 1 for the closed 40 00:03:00,803 --> 00:03:06,890 upper end point which is 32. Now what are these going to be? 41 00:03:06,890 --> 00:03:10,380 Well, I'm supposed to produce true if the given seat number is on the aisle. 42 00:03:10,380 --> 00:03:12,640 Lets see. This one is on the aisle. 43 00:03:12,640 --> 00:03:19,030 This one is not. And this one is. 44 00:03:19,030 --> 00:03:23,450 Notice how much information the data definition gave me about generating the 45 00:03:23,450 --> 00:03:25,909 test. Data definitions are always going to be 46 00:03:25,909 --> 00:03:28,900 helpful this way. They're not always going to make it quite 47 00:03:28,900 --> 00:03:32,735 this straightforward but data definitions, the structure of the data 48 00:03:32,735 --> 00:03:38,927 definition, the structure of the data tells us a lot about the test we need. 49 00:03:38,927 --> 00:03:43,890 So, let me run that. Good, they're all running. 50 00:03:45,530 --> 00:03:48,590 Now for the template, what I'm going to do is, I'm first going to come in off the 51 00:03:48,590 --> 00:03:52,606 stub. I'm going to say use template from 52 00:03:52,606 --> 00:04:00,431 SeatNum and I'll just leave that note or copy the template down. 53 00:04:00,431 --> 00:04:06,757 You know, use it like that. I have to rename it to the aisle, that's 54 00:04:06,757 --> 00:04:11,467 the name of this function. And let's see. 55 00:04:11,467 --> 00:04:14,950 I'm consuming a seat number, which is a natural from 1 to 32. 56 00:04:14,950 --> 00:04:17,850 And I need to produce true if it's an aisle seat. 57 00:04:17,850 --> 00:04:22,423 That means, I need to produce true if SeatNum is 1. 58 00:04:22,423 --> 00:04:28,310 Also SeatNum is 32, and I just need to order those two together. 59 00:04:28,310 --> 00:04:33,139 Let's try that. All three tests passed. 60 00:04:33,139 --> 00:04:38,078 So, there you go. That's designing a function that consumes 61 00:04:38,078 --> 00:04:40,719 an interval. And the big piece of help we got from 62 00:04:40,719 --> 00:04:44,373 knowing it's an interval is, well, of course we got the template, but we also 63 00:04:44,373 --> 00:04:49,464 got the structure of the tests. In this case, it's an interval with two 64 00:04:49,464 --> 00:04:51,870 closed ends. So, we have a test for each of the ends 65 00:04:51,870 --> 00:04:53,930 and a test for somewhere in the middle.