1 00:00:06,460 --> 00:00:09,630 As you know, this week is about designing data. 2 00:00:09,630 --> 00:00:13,220 And as part of that, we're going to introduce a new kind of design element 3 00:00:13,220 --> 00:00:17,130 called the data definition. What I'm going to do in this video 4 00:00:17,130 --> 00:00:21,650 though, is just talk about the problem that data definition solve. 5 00:00:21,650 --> 00:00:26,220 And the way I'm going to do it is first show you how to design functions recipe 6 00:00:26,220 --> 00:00:30,930 solution, a complete function design. Look at a problem we have in 7 00:00:30,930 --> 00:00:35,320 understanding that, and then show you a complete data definition and talk about 8 00:00:35,320 --> 00:00:39,510 how it solves the problem. Then in the next video we'll look at 9 00:00:39,510 --> 00:00:43,480 designing data definitions from scratch. So now suppose you're working on a 10 00:00:43,480 --> 00:00:47,630 program that somebody else wrote, and the program has to do with simulating 11 00:00:47,630 --> 00:00:51,090 traffic. So it's reasonable to expect in a program 12 00:00:51,090 --> 00:00:54,910 like that, that there's going to be code that has to do with traffic light and 13 00:00:54,910 --> 00:01:01,010 cars and trucks and things like that. Now you're reading that program and you 14 00:01:01,010 --> 00:01:07,148 come to this function definition, and you ask yourself, what does it do? 15 00:01:07,148 --> 00:01:10,150 Well, you know, the name is kind of a hint. 16 00:01:10,150 --> 00:01:16,250 It seems to produce the next color. So maybe if you give it a color, it gives 17 00:01:16,250 --> 00:01:20,080 you another color. But it's just not really very clear from 18 00:01:20,080 --> 00:01:22,240 this. Now at this point in the course, you 19 00:01:22,240 --> 00:01:25,900 might say, well, look, the problem is that this person didn't follow the how to 20 00:01:25,900 --> 00:01:29,558 design functions recipe. Let's imagine they did follow how to 21 00:01:29,558 --> 00:01:35,110 design functions recipe. So here's the same function with the how 22 00:01:35,110 --> 00:01:40,070 to define functions recipe. There's the original function definition. 23 00:01:40,070 --> 00:01:43,090 And there we see that the function consumes the natural and produces the 24 00:01:43,090 --> 00:01:47,035 natural and produces the next color of traffic light. 25 00:01:47,035 --> 00:01:51,300 But here is a question. The signature says the function consumes 26 00:01:51,300 --> 00:01:57,770 natural, so can I call it with 3? 3 is a natural number, but I don't see 27 00:01:57,770 --> 00:02:02,100 any examples other than 0, 1, or 2. And what do these examples mean anyways? 28 00:02:02,100 --> 00:02:06,289 I mean the examples match the functions, so they kind of tell me the function's 29 00:02:06,289 --> 00:02:09,950 probably right. But when I call next-color with 0 and it 30 00:02:09,950 --> 00:02:14,377 gives me back a 2, what does that mean? Well in order to understand this problem, 31 00:02:14,377 --> 00:02:19,580 we have to understand something very important about data in programs. 32 00:02:19,580 --> 00:02:23,020 In any program we have what we call the problem domain. 33 00:02:23,020 --> 00:02:27,490 In this case the problem domain Has to do with traffic and simulation of traffic, 34 00:02:27,490 --> 00:02:30,570 and traffic lights, and cars, and roads, and things like that. 35 00:02:30,570 --> 00:02:34,310 And in the problem domain we have information in the problem domain. 36 00:02:34,310 --> 00:02:38,560 In this case we might have a piece of information like a certain light is red. 37 00:02:38,560 --> 00:02:42,020 But inside the program we don't have red lights, we don't green lights, we don't 38 00:02:42,020 --> 00:02:46,840 have lights at all inside the program. What we have inside the program is data, 39 00:02:46,840 --> 00:02:52,170 in this case the natural number 0. And what's going on, when we design a 40 00:02:52,170 --> 00:02:59,040 program like this, is that we are playing a game, where we represent information in 41 00:02:59,040 --> 00:03:02,210 the problem domain using data in the program. 42 00:03:02,210 --> 00:03:09,020 In this case we represent the fact that some light might be read using the 43 00:03:09,020 --> 00:03:15,930 natural number 0, and the inverse of that relationship is that we can interpret the 44 00:03:15,930 --> 00:03:20,420 natural number 0 if we are told this natural number is supposed to represent 45 00:03:20,420 --> 00:03:24,870 some information about a light, we can interpret it as meaning the light is red. 46 00:03:24,870 --> 00:03:31,324 And now if I tell you this, if I tell you hey, to represent a red light, you use 0, 47 00:03:31,324 --> 00:03:35,500 and if 0 is representing a light, it means it's red. 48 00:03:35,500 --> 00:03:37,620 Then you can start to understand this program a bit more. 49 00:03:37,620 --> 00:03:44,802 You can look at it and you can say, well if 0 is red, then I guess maybe 1 is 50 00:03:44,802 --> 00:03:47,970 yellow. But instead of giving this information in 51 00:03:47,970 --> 00:03:51,400 a piecemeal way, what we're going to do is we're going to use something called a 52 00:03:51,400 --> 00:03:56,670 data definition, and a data definition is really going to tell us everything that 53 00:03:56,670 --> 00:04:01,160 we need to know about how we represent information as data. 54 00:04:01,160 --> 00:04:04,470 So now lets look at a new version of this program, the third version of it. 55 00:04:04,470 --> 00:04:08,001 That uses the data definition. In this third version of the traffic 56 00:04:08,001 --> 00:04:12,322 program we divided the file into two parts, the first part is called data 57 00:04:12,322 --> 00:04:15,205 definitions and the second part is called functions. 58 00:04:15,205 --> 00:04:18,370 And in the data definitions part we have one data definition. 59 00:04:18,370 --> 00:04:25,250 I want to read this quickly now and in future videos this week we'll talk about 60 00:04:25,250 --> 00:04:28,920 it in more detail. But the data definition starts out with a 61 00:04:28,920 --> 00:04:34,480 type comment that says there's a new type, TLColor, and the way you form 62 00:04:34,480 --> 00:04:38,810 something of TLColor, is that it's either 0, 1, or 2. 63 00:04:38,810 --> 00:04:41,350 We'll have more elaborate type comments in the next video. 64 00:04:43,100 --> 00:04:47,740 And it says, that if you have a piece of this data, you interpret it as follows, 65 00:04:47,740 --> 00:04:53,340 something that's a TL color is the color of a traffic light, and 0 means red, 1 66 00:04:53,340 --> 00:05:00,330 means yellow, and 2 means green. And this last part is a template for 67 00:05:00,330 --> 00:05:04,690 operating on TL color. And for now just read it as saying, if 68 00:05:04,690 --> 00:05:07,946 you're designing a function that operates on TL color. 69 00:05:07,946 --> 00:05:12,640 Then there is really three cases, there is one case where the color is 0, there 70 00:05:12,640 --> 00:05:16,106 is one case where the color is 1 and there is one case where the color is 2 71 00:05:16,106 --> 00:05:21,840 and you know all about TLColor. Now, if you scroll down a little bit 72 00:05:21,840 --> 00:05:26,630 there will be a functions part, here is a function and the signature now says hey, 73 00:05:26,630 --> 00:05:31,580 this function consumes the TLColor and it produces the TLColor. 74 00:05:31,580 --> 00:05:35,380 And notice we've already answered one of our questions from before, this function 75 00:05:35,380 --> 00:05:41,860 can't consume a 4 or a 3 because it can only consume a TLColor. 76 00:05:41,860 --> 00:05:46,760 And the only thing a TLColor can be, I can see by looking up here at the type 77 00:05:46,760 --> 00:05:50,220 comment, a TLColor can only be a 0, a 1, or a 2. 78 00:05:50,220 --> 00:05:55,230 And this function can only produce a TLcolor, a zero a one or a two and now 79 00:05:55,230 --> 00:05:59,200 its producing the next color of a traffic light and now this function gets to be 80 00:05:59,200 --> 00:06:03,510 easier to understand. If I say next color of a zero which means 81 00:06:03,510 --> 00:06:08,500 red then that produces a two which means green and that's right. 82 00:06:08,500 --> 00:06:12,880 After a light is red its green. That's color number 1, which is means 83 00:06:12,880 --> 00:06:20,500 yellow, is a 0, which means red. And a light also gos from green to 84 00:06:20,500 --> 00:06:22,870 yellow. And now the template, well what's the 85 00:06:22,870 --> 00:06:26,344 template for this function. Well the template came from TLColor. 86 00:06:26,344 --> 00:06:32,270 Because this function consumes a tl color and what we're going to see for the next 87 00:06:32,270 --> 00:06:37,560 several weeks is that the template of a function is determined by what type data 88 00:06:37,560 --> 00:06:43,330 it consumes and this just says well if you consume a zero, in other words a red 89 00:06:43,330 --> 00:06:46,450 light, produce a zero. You can see in the one or in other words 90 00:06:46,450 --> 00:06:52,470 the yellow light produce a red light and a green light goes to a yellow light. 91 00:06:52,470 --> 00:06:54,940 There is a couple of important things going on here. 92 00:06:54,940 --> 00:07:00,270 One is that this data definition tells me how to represent information as data and 93 00:07:00,270 --> 00:07:03,920 how to interpret data as information. This data definition has made this 94 00:07:03,920 --> 00:07:08,725 program much more meaningful. And this data definition has also greatly 95 00:07:08,725 --> 00:07:14,673 simplified the design of this function. Because the signature now says TLColor to 96 00:07:14,673 --> 00:07:18,520 TLColor, I know all sorts of things this function can't consume. 97 00:07:18,520 --> 00:07:24,120 It can't consume the number 3, and I also know how to read this function and 98 00:07:24,120 --> 00:07:29,280 understand what it's doing. So that's what data definitions are 99 00:07:29,280 --> 00:07:31,605 going to do for us. They're going to help us really 100 00:07:31,605 --> 00:07:35,760 understand the relationship between information in data and how data 101 00:07:35,760 --> 00:07:40,140 represents information, and how data can be interpreted as information. 102 00:07:41,190 --> 00:07:44,810 But I didn't talk about how to design data definitions, and I didn't talk about 103 00:07:44,810 --> 00:07:49,680 how to design a function that consumes data defined by a data definition. 104 00:07:49,680 --> 00:07:52,290 I just showed you examples of the completed product. 105 00:07:53,780 --> 00:07:58,390 So now what we're going to do starting next time is look at how to design data definitions.