1 00:00:06,760 --> 00:00:10,570 Now we're going to work through an example of designing a data definition from 2 00:00:10,570 --> 00:00:13,250 scratch using the How to Design Data 3 00:00:13,250 --> 00:00:17,110 recipe and the accompanying Data-Driven Templates recipe. 4 00:00:18,360 --> 00:00:20,170 It's going to be a simpler example than the 5 00:00:20,170 --> 00:00:23,530 traffic light example you saw earlier in the week. 6 00:00:23,530 --> 00:00:26,190 What we're going to do over the next couple of lessons is 7 00:00:26,190 --> 00:00:30,430 build up our skills to desgining more complicated data definitions like that. 8 00:00:31,890 --> 00:00:32,010 What 9 00:00:32,010 --> 00:00:35,400 I've got is the city name starter from the week two page. 10 00:00:37,100 --> 00:00:39,830 Is says imagine that I designed a program that, among other 11 00:00:39,830 --> 00:00:44,160 things, has information about the names of cities in its probability. 12 00:00:44,160 --> 00:00:46,800 Design a data definition to represent the name of the city. 13 00:00:46,800 --> 00:00:47,300 So 14 00:00:48,930 --> 00:00:52,330 the key first thing that we do when we design a data definition 15 00:00:52,330 --> 00:00:55,740 is to work out the form of the information they're trying to represent. 16 00:00:56,830 --> 00:00:59,130 And it might even be good to just write down a couple examples. 17 00:00:59,130 --> 00:01:00,910 Maybe I'll put a little Comment Box right here. 18 00:01:03,950 --> 00:01:07,940 And I'll write some examples. So one example city name is Vancouver. 19 00:01:07,940 --> 00:01:10,470 And another example city name, we have 20 00:01:10,470 --> 00:01:13,740 Boston for example, and these are all information. 21 00:01:13,740 --> 00:01:14,240 And 22 00:01:18,600 --> 00:01:20,500 now the question is, how am I going to represent that? 23 00:01:20,500 --> 00:01:23,020 What I'm going to do is go to the court's 24 00:01:23,020 --> 00:01:25,379 website and I'm going to go to the design recipes page. 25 00:01:26,570 --> 00:01:29,480 And this again provides me an index into all the design recipes. 26 00:01:30,570 --> 00:01:33,200 And I'll go to the How to Design Data Recipe. 27 00:01:35,520 --> 00:01:37,820 In this web page I won't go through in detail now, I'm just 28 00:01:37,820 --> 00:01:40,689 going to work through it by example but you should review it on your own. 29 00:01:41,910 --> 00:01:45,790 It tells me about the basic parts of every data definition. 30 00:01:45,790 --> 00:01:47,360 And I'll pick this up later and set it 31 00:01:47,360 --> 00:01:49,495 down off to the right when we're working in Dr. 32 00:01:49,495 --> 00:01:49,520 [UNKNOWN]. 33 00:01:49,520 --> 00:01:54,450 And the key thing it helps me do, is decide what kind of data 34 00:01:54,450 --> 00:01:58,990 definition to use Based on the form of the information that we need to represent. 35 00:02:00,680 --> 00:02:02,040 Now remember we're trying to represent 36 00:02:02,040 --> 00:02:04,570 city names things like Vancouver and Boston. 37 00:02:05,910 --> 00:02:09,590 And the thing about that information is that it's atomic And what I mean by 38 00:02:09,590 --> 00:02:14,530 atomic is, you can't take it apart into pieces that are still meaningfully part of 39 00:02:14,530 --> 00:02:17,740 the same problem domain. So yes, it's true. 40 00:02:17,740 --> 00:02:25,030 I can take the city name Boston apart into the letters b o s It's TON. 41 00:02:25,030 --> 00:02:27,680 But those aren't meaningfully part of a city name, in a 42 00:02:27,680 --> 00:02:30,760 sense, they're not like neighborhoods of the city or something like that. 43 00:02:31,900 --> 00:02:40,480 So this information is atomic, and the way we represent atomic information 44 00:02:40,480 --> 00:02:42,370 is with a type comment that looks like this. 45 00:02:42,370 --> 00:02:44,040 We simply have a new type name, 46 00:02:46,420 --> 00:02:52,120 and we say what underlying primitive atomic data it's going to represent it as. 47 00:02:52,120 --> 00:02:58,250 So going back over here to DrRacket, I'm going to say, 48 00:02:58,250 --> 00:03:04,190 Cityname is String. A good way to represent this information. 49 00:03:04,190 --> 00:03:11,830 He's using strings, so for example, if you do this the data that represents the 50 00:03:11,830 --> 00:03:16,440 city name Vancouver is going to be the string Vancouver. 51 00:03:17,600 --> 00:03:22,010 And The data that represents the city name Boston is going to be the string Boston. 52 00:03:23,890 --> 00:03:25,230 So here's my type comments. 53 00:03:25,230 --> 00:03:27,290 So the name is string and now I need 54 00:03:27,290 --> 00:03:31,400 to provide interpretations so I type interpret and for 55 00:03:31,400 --> 00:03:31,400 [UNKNOWN] 56 00:03:31,400 --> 00:03:35,580 atomic data the data interpretation is often fairly simple, especially for a 57 00:03:35,580 --> 00:03:40,980 case like this We're just going to say the name of a city. 58 00:03:44,340 --> 00:03:47,410 Now, the next part of the data definition is something that 59 00:03:47,410 --> 00:03:50,060 we didn't see in the last example, and we'll talk about 60 00:03:50,060 --> 00:03:53,230 why that is more in a couple videos, but we want 61 00:03:53,230 --> 00:03:56,949 to provide some examples of how the data definition represents information. 62 00:03:58,130 --> 00:03:59,500 So what I'm going to do is I'm going to say 63 00:03:59,500 --> 00:04:04,700 Define city name one is, well one good example is Boston. 64 00:04:06,930 --> 00:04:12,090 And define city name two, and another good example is 65 00:04:12,090 --> 00:04:17,630 Vancouver. Now, I'll be honest with you. 66 00:04:17,630 --> 00:04:20,780 Providing examples of really simple data definitions like 67 00:04:20,780 --> 00:04:24,060 this doesn't do that much work, but again 68 00:04:24,060 --> 00:04:26,400 we're learning the recipe now so that it 69 00:04:26,400 --> 00:04:28,770 will help us more with more complicated examples. 70 00:04:29,790 --> 00:04:32,470 And fairly soon we'll have data definitions Where 71 00:04:32,470 --> 00:04:35,780 the example's really are going to help us understand the data definitions better. 72 00:04:36,820 --> 00:04:37,990 So there's the examples. 73 00:04:39,840 --> 00:04:46,330 The next step of the recipe is to produce the template for this data definition. 74 00:04:47,950 --> 00:04:51,330 Now to produce the template, what I do, I go back to the course website, 75 00:04:51,330 --> 00:04:57,850 and I go to the design recipes page, and I get the data driven templates rules. 76 00:04:57,850 --> 00:05:00,850 So here's the data driven templates recipe. 77 00:05:03,130 --> 00:05:04,650 And the way it works is it says for a 78 00:05:04,650 --> 00:05:07,900 given type name, the data driven template looks like this. 79 00:05:07,900 --> 00:05:08,400 So 80 00:05:10,490 --> 00:05:16,880 what I do is I say define fun for city name 81 00:05:16,880 --> 00:05:20,240 and we're going to be quite particular about the name of the template. 82 00:05:20,240 --> 00:05:23,660 Naming the template directly after the type name is going to turn 83 00:05:23,660 --> 00:05:26,380 out to help us Quite a bit later on in the course. 84 00:05:27,600 --> 00:05:30,230 And I'll pick as a parameter name for this template, CN. 85 00:05:30,230 --> 00:05:34,450 And now I've gotta do something about the body here. 86 00:05:36,190 --> 00:05:37,340 And what I'm also going to do is 87 00:05:37,340 --> 00:05:42,280 I'm going to write template rules used, like this. 88 00:05:43,420 --> 00:05:48,070 And now looking back at the data-driven templates page, What I see is 89 00:05:48,070 --> 00:05:52,190 the way to produce the body is to follow the rules in this table. 90 00:05:52,190 --> 00:05:54,450 What I do is go to the type comment. 91 00:05:54,450 --> 00:05:59,260 I look at the first word after is, so that's string. 92 00:05:59,260 --> 00:06:01,210 And then I start working in this table 93 00:06:01,210 --> 00:06:01,850 from there. 94 00:06:03,300 --> 00:06:06,830 Now string is what's called an atomic non-distinct type. 95 00:06:06,830 --> 00:06:10,530 The best way to understand what non distinct means is to wait 96 00:06:10,530 --> 00:06:13,470 for a couple more videos and then we'll see what distinct examples are. 97 00:06:14,610 --> 00:06:18,960 But string is right here, so this is the row of the table I'm going to use. 98 00:06:21,690 --> 00:06:24,070 And I'm not inside of a cond, you'll see what it 99 00:06:24,070 --> 00:06:26,980 means to be inside a cond again in a couple of videos. 100 00:06:28,230 --> 00:06:30,460 But for the body, I'm just going to say dot 101 00:06:30,460 --> 00:06:34,510 dot dot x where x is the actual parameter name. 102 00:06:34,510 --> 00:06:41,384 So the body of this template is just going to be ... 103 00:06:41,384 --> 00:06:43,460 cn. 104 00:06:43,460 --> 00:06:47,990 And I'm also going to write here, the template rule that I used, and 105 00:06:47,990 --> 00:06:55,526 the rule that I used was the atomic non-distinct rule. 106 00:06:55,526 --> 00:06:55,534 [SOUND] 107 00:06:55,534 --> 00:06:58,610 And I'm also going to write the part of the type 108 00:06:58,610 --> 00:07:02,590 comment that matched that rule, which in this case is string. 109 00:07:05,910 --> 00:07:09,650 And now I'm going to run this and there's nothing really to 110 00:07:09,650 --> 00:07:12,660 run but the key thing is I didn't get any errors. 111 00:07:13,780 --> 00:07:16,160 If for example I had a mistake like I left a 112 00:07:16,160 --> 00:07:20,020 parenthesis off there then I would get an error right now. 113 00:07:21,120 --> 00:07:24,700 And I would fix this poorly formed template right now. 114 00:07:25,860 --> 00:07:28,290 So I'll fix this poorly formed template right now. 115 00:07:30,680 --> 00:07:32,450 And now I've got this running properly. 116 00:07:33,920 --> 00:07:35,360 And what I'll do is I'll comment out the 117 00:07:35,360 --> 00:07:38,740 template, because we're never actually going to call this template function. 118 00:07:38,740 --> 00:07:41,700 We're just going to copy it into later function design. 119 00:07:41,700 --> 00:07:46,270 And I'll get rid of this now, this scratch work. 120 00:07:46,270 --> 00:07:51,030 Because that's already really captured in the data definition. 121 00:07:53,800 --> 00:07:58,210 So there you go, I've completed a first data definition by following the how to 122 00:07:58,210 --> 00:08:04,430 design data recipe using also the data driven templates recipe. 123 00:08:06,130 --> 00:08:10,370 In the next video we will define a function that uses this data definition.