1 00:00:00,008 --> 00:00:08,910 Here is another how to design data problem. 2 00:00:08,910 --> 00:00:11,670 In this case, it's going to be for an enumeration. 3 00:00:11,670 --> 00:00:15,600 So, the data definition that we produce will be similar to the traffic light 4 00:00:15,600 --> 00:00:20,295 color example from earlier in the week. This is the problem in 5 00:00:20,295 --> 00:00:25,040 letter-grade-starter.rkt, and again I know this is a data design problem. 6 00:00:25,040 --> 00:00:29,630 So, I've got the how to design data recipe here and I've got the how to 7 00:00:29,630 --> 00:00:32,910 design data web page open in my browser here. 8 00:00:32,910 --> 00:00:35,447 We're designing a system to keep track of student grades. 9 00:00:35,447 --> 00:00:40,630 And we need to design a data definition to represent the letter grade in a course 10 00:00:40,630 --> 00:00:45,620 which is one of A, B, or C. The key thing about the information in 11 00:00:45,620 --> 00:00:49,211 this problem is that there's three distinct values. 12 00:00:49,211 --> 00:00:54,390 In other words, every letter grade is either going to be the distinct value A, 13 00:00:54,390 --> 00:00:57,140 or the distinct value B, or the distinct value C. 14 00:00:57,140 --> 00:01:02,576 It isn't like something where we might be keeping track of the numerical score on a 15 00:01:02,576 --> 00:01:04,610 course, which is a whole bunch of numbers. 16 00:01:04,610 --> 00:01:09,189 Here, there's really a modest number of distinct values A, B or C. 17 00:01:13,570 --> 00:01:15,870 And when the problem domain information is like that. 18 00:01:15,870 --> 00:01:21,150 When it consists of a fixed number of distinct values, then there's a special 19 00:01:21,150 --> 00:01:24,185 kind of data definition to use for that called enumeration. 20 00:01:24,185 --> 00:01:31,015 In the way enumeration works is that we're going to use one of, in the type 21 00:01:31,015 --> 00:01:35,570 comment, this way. Now, this is exactly like the traffic 22 00:01:35,570 --> 00:01:39,120 light data definition from earlier this week. 23 00:01:39,120 --> 00:01:44,317 So, here we go. I'm going to say, LetterGrade is one of. 24 00:01:44,317 --> 00:01:51,600 And now, what I'm going to do is I'm going to talk about the data used to 25 00:01:51,600 --> 00:01:57,350 represent each of the distinct cases. And I'll use strings in this case, I'll 26 00:01:57,350 --> 00:02:06,107 say that these are the three distinct data values which are going to represent 27 00:02:06,107 --> 00:02:12,870 the three distinct information values. And when you do an enumeration, the 28 00:02:12,870 --> 00:02:16,310 interpretation tends to be relatively straightforward. 29 00:02:16,310 --> 00:02:20,760 In this case, you'll just say the letter grade in a course. 30 00:02:20,760 --> 00:02:27,490 But if you think back to that example we saw at the beginning of the week. 31 00:02:27,490 --> 00:02:33,016 If, for example, you have decided to use the three data values, maybe numbers like 32 00:02:33,016 --> 00:02:41,130 0, 1, and 2, well then, your interpretation would be more substantive. 33 00:02:41,130 --> 00:02:46,322 Then your interpretation would have to say something like, you know, 0 means A, 34 00:02:46,322 --> 00:02:52,370 1 means B, 2 means C. So, part of why the interpenetration is 35 00:02:52,370 --> 00:02:59,660 simple in this case is because we're using strings to represent the three 36 00:02:59,660 --> 00:03:02,290 cases. And the strings will clearly say what the 37 00:03:02,290 --> 00:03:06,038 cases are. The other thing that happens in 38 00:03:06,038 --> 00:03:09,600 enumerations is that examples are kind of silly. 39 00:03:09,600 --> 00:03:12,590 I mean, if we tried to write examples for this, what would we write? 40 00:03:12,590 --> 00:03:19,080 We'd write something like define letter grade 1 as A means A. 41 00:03:19,080 --> 00:03:30,718 Define letter grade 2 as B means B. Define letter grade 3 as C means, means 42 00:03:30,718 --> 00:03:33,320 C. It's kind of silly, right? 43 00:03:33,320 --> 00:03:36,170 Its ridiculous. Because it's an enumeration there's only 44 00:03:36,170 --> 00:03:38,730 three values. We know what the examples are before we 45 00:03:38,730 --> 00:03:42,480 get to the example stage. So, we don't even bother to write them. 46 00:03:42,480 --> 00:03:49,640 What we write here is we say examples are redundant for enumerations. 47 00:03:49,640 --> 00:03:52,910 And once we get a couple weeks farther in the course, you don't even have to write 48 00:03:52,910 --> 00:03:55,890 that. We want you to write it for now, just as 49 00:03:55,890 --> 00:03:59,313 a way of remembering that the example step existed. 50 00:03:59,313 --> 00:04:09,067 Now, we're going to do the template. So, define fn-for-letter-grade, grade, 51 00:04:09,067 --> 00:04:10,868 lg. There's going to be some body here, 52 00:04:10,868 --> 00:04:18,550 template rules used. And here we go. 53 00:04:19,870 --> 00:04:24,115 I'll go back over to the How to Design Data web page, here we were. 54 00:04:24,115 --> 00:04:31,387 And I'll scroll up to find a link to the Data Driven Templates recipe. 55 00:04:31,387 --> 00:04:36,850 And, here we go. So, I need to look in this column for the 56 00:04:36,850 --> 00:04:41,255 first word after is. Now remember, the first word after is is 57 00:04:41,255 --> 00:04:46,270 1 of. So, it's not any of these, there's a 58 00:04:46,270 --> 00:04:50,950 special rule for one of. And one ofs are always in numerations 59 00:04:50,950 --> 00:04:54,070 like this case is. One of itemization which is the example 60 00:04:54,070 --> 00:04:57,585 we are doing after this. And now, this says that the body is a 61 00:04:57,585 --> 00:05:01,360 cond with one clause per subclass of the one of. 62 00:05:01,360 --> 00:05:04,220 What does that mean? What's the subclass of the one of? 63 00:05:04,220 --> 00:05:09,930 What's going on here in this one of is this three cases or three sub-classes. 64 00:05:09,930 --> 00:05:16,510 This says every LetterGrade in the world is either, in this class, in this class, 65 00:05:16,510 --> 00:05:18,680 or in this class. Every LetterGrade in the world is either 66 00:05:18,680 --> 00:05:24,159 an A, a B, or a C. So, here we go, it says that we're 67 00:05:24,159 --> 00:05:29,160 going to make a cond with one clause per sub-class of the one of, so let me just 68 00:05:29,160 --> 00:05:34,800 do that. I'm going to say cond, and there's 69 00:05:34,800 --> 00:05:38,610 going to be three of these, because there's three there, so there'll be three 70 00:05:38,610 --> 00:05:47,080 here. And down here in my Template rules, I'm 71 00:05:47,080 --> 00:05:53,800 going to put that we used the one of rule and that there are 3 cases. 72 00:05:53,800 --> 00:05:57,580 Now, what happens, well now, I've gotten past the one of. 73 00:05:57,580 --> 00:06:01,850 And the next thing I see is an A. The next thing I see in fact, in fact, is 74 00:06:01,850 --> 00:06:07,100 the string A. So, which template rule is that? 75 00:06:07,100 --> 00:06:11,610 Because what this says is that, for each question and answer expression, I'm 76 00:06:11,610 --> 00:06:16,160 going to form it by following the rule in the question or answer column of this 77 00:06:16,160 --> 00:06:22,880 table for the corresponding case. So, I'm going to this A, and I need to 78 00:06:22,880 --> 00:06:27,680 find its row in this table, and A is an atomic distinct value. 79 00:06:27,680 --> 00:06:30,990 Now, you understand what the distinct value rule that we were just skipping 80 00:06:30,990 --> 00:06:35,480 over before was. There's a single value, the string A. 81 00:06:35,480 --> 00:06:39,240 So, it's this case. And now, you also understand what the 82 00:06:39,240 --> 00:06:42,270 conned question column of this table was for. 83 00:06:43,720 --> 00:06:46,685 It says that in the question column, we're going to put the appropriate 84 00:06:46,685 --> 00:06:50,430 predicate. Which is a string equal in this case. 85 00:06:50,430 --> 00:06:56,698 So, we'll go back over here and we'll say string=? 86 00:06:56,698 --> 00:07:03,780 letter grade to A. That's the question. 87 00:07:03,780 --> 00:07:13,326 And in the cond answer, we're going to put just dot dot dot. 88 00:07:13,326 --> 00:07:17,451 Now, you might ask why aren't we putting dot dot dot letter grade. 89 00:07:17,451 --> 00:07:19,975 Well, two answers. One answer is the table, is the table 90 00:07:19,975 --> 00:07:23,383 told us to put just dot dot dot. It didn't tell us to put dot dot dot and 91 00:07:23,383 --> 00:07:26,944 the name of the parameter. It told us to just put dot dot dot, but 92 00:07:26,944 --> 00:07:30,206 the question is why does the table say that? 93 00:07:30,206 --> 00:07:34,044 Well, the table says that because in this case we know what lg is. 94 00:07:34,044 --> 00:07:39,355 lg is the string A. It'll always be the string A if we get to 95 00:07:39,355 --> 00:07:43,102 this cond answer. So, we're not putting it there to make 96 00:07:43,102 --> 00:07:47,944 that more clear. And so, the template rule that we used 97 00:07:47,944 --> 00:07:55,340 here was atomic distinct value A, now we go on to the next row. 98 00:07:55,340 --> 00:07:59,814 Well, the next row is the string B, so we could go back to the table. 99 00:07:59,814 --> 00:08:03,846 Let's see the string B is an atomic distinct value, so the cond question will 100 00:08:03,846 --> 00:08:08,174 be an appropriate predicate. And the cond answer will be open 101 00:08:08,174 --> 00:08:11,285 parenthesis dot dot dot close round bracket. 102 00:08:11,285 --> 00:08:23,047 So, let's see the cond question. Is string=?, letter grade to B. 103 00:08:23,047 --> 00:08:35,096 Then, the cond answer is dot, dot, dot. And this is atomic, distinct value B. 104 00:08:35,096 --> 00:08:37,742 And at this point you can see how this is working and you can short circuit the 105 00:08:37,742 --> 00:08:43,285 process of doing C a bit if you want. We'll just copy the case for B. 106 00:08:43,285 --> 00:08:46,402 We'll get rid of this last question and answer pair. 107 00:08:46,402 --> 00:08:56,912 We'll make that to C, and we can do that down here in the template rules as well. 108 00:08:56,912 --> 00:09:00,641 Now, we'll run it. There's no errors. 109 00:09:00,641 --> 00:09:06,171 So, it's all well formed. We'll comment that out. 110 00:09:06,171 --> 00:09:12,801 So, this is an enumeration because the domain information here has two or more 111 00:09:12,801 --> 00:09:20,597 distinct values A, B and C. And an enumeration data definition uses a 112 00:09:20,597 --> 00:09:26,159 one of with the data values that are going to be used to represent the 113 00:09:26,159 --> 00:09:31,757 information. And now we've seen two new template 114 00:09:31,757 --> 00:09:36,019 rules, the one on rule and the distinct value rule. 115 00:09:36,019 --> 00:09:38,410 So, there we go. That's our data definition for letter grade.