1 00:00:06,320 --> 00:00:10,096 In this how to design data problem, the type comment is going to end up with the 2 00:00:10,096 --> 00:00:13,968 one of. But not all of the subclasses are going 3 00:00:13,968 --> 00:00:18,138 to be single distinct values. So this is what's going to be called an 4 00:00:18,138 --> 00:00:20,980 itemization. And it'll be the most complex form of 5 00:00:20,980 --> 00:00:26,100 data that we see this week. This is the problem in countdown starter. 6 00:00:26,100 --> 00:00:29,492 So what we're supposed to do here is design a data definition to represent the 7 00:00:29,492 --> 00:00:33,100 current state of a New Year's Eve countdown. 8 00:00:33,100 --> 00:00:35,990 So this is something like the countdown ball in Times Square. 9 00:00:35,990 --> 00:00:38,450 And we're asked to design a data definition for that. 10 00:00:39,850 --> 00:00:42,656 As always, when we design a data definition, the first thing is to clearly 11 00:00:42,656 --> 00:00:46,970 identify the information in the problem domain that we're trying to represent. 12 00:00:48,150 --> 00:00:51,070 And in this case, the problem statement makes that very clear to us. 13 00:00:51,070 --> 00:00:54,846 In particular, it makes it clear that the information falls into one of three 14 00:00:54,846 --> 00:00:58,750 categories. The count down hasn't started. 15 00:00:58,750 --> 00:01:02,120 It's from ten to one seconds before midnight or it's complete. 16 00:01:02,120 --> 00:01:07,424 The previous problem we saw, the letter grade problem, also had three categories 17 00:01:07,424 --> 00:01:14,160 and here the categories are different. This first one is kind of distinct. 18 00:01:14,160 --> 00:01:18,940 The count down hasn't started. That's a clear, distinct state. 19 00:01:18,940 --> 00:01:23,675 But in this second category, there's not a single distinct value there. 20 00:01:23,675 --> 00:01:28,268 There's 10, 9, 8, all the way to 1. So there's a number of different values 21 00:01:28,268 --> 00:01:31,231 there. And then in the third category, again, 22 00:01:31,231 --> 00:01:35,501 there's a distinct state there. The countdown is complete. 23 00:01:35,501 --> 00:01:41,029 So this isn't going to be an enumeration. This is going to be what's called an 24 00:01:41,029 --> 00:01:45,585 itemization because there are two or more sub classes, at least one of which is not 25 00:01:45,585 --> 00:01:51,036 a single distinct value. This center one is not a single distinct 26 00:01:51,036 --> 00:01:54,370 value, so that's going to be an itemization. 27 00:01:54,370 --> 00:01:58,465 And again after this video is over I encourage you to go to the Design Recipes 28 00:01:58,465 --> 00:02:03,938 page, go to the How to Design Data page. This table will remind you that 29 00:02:03,938 --> 00:02:08,030 itemization is what you need to use in a case like this and you can go and read 30 00:02:08,030 --> 00:02:13,143 about the itemizations. Do that carefully after this video is 31 00:02:13,143 --> 00:02:16,112 over. I'll work through the problem now. 32 00:02:16,112 --> 00:02:19,320 So let's see. We start with the type comment as always, 33 00:02:19,320 --> 00:02:27,266 so we'll say that countdown, is one of. And there's three subclasses. 34 00:02:27,266 --> 00:02:31,231 And now we have to decide for each subclass how we'll represent that, what 35 00:02:31,231 --> 00:02:35,839 data will represent that. Well let's see, if the countdown hasn't 36 00:02:35,839 --> 00:02:39,710 yet started that's kind of a distinct state. 37 00:02:39,710 --> 00:02:43,580 So we could use any piece of atomic distinct data to represent that. 38 00:02:43,580 --> 00:02:47,990 Let's just use false. Then we will use the Boolean value false. 39 00:02:47,990 --> 00:02:50,209 To represent that the countdown hasn't yet started. 40 00:02:52,430 --> 00:02:57,254 Now, in the second subclass, let's just say that that'll be a natural number from 41 00:02:57,254 --> 00:03:02,201 one to ten, inclusive. In the third case, we also need a 42 00:03:02,201 --> 00:03:09,347 distinct value there and we'll just use the string, in this case, complete. 43 00:03:11,040 --> 00:03:15,249 So now, we've got three distinct sub classes that correspond to the three 44 00:03:15,249 --> 00:03:20,264 distinct kinds of information that need to be represented. 45 00:03:20,264 --> 00:03:25,871 Well, we'll do an interpretation here, and I'll use multiple lines for this 46 00:03:25,871 --> 00:03:32,657 interpretation. I'll say false means countdown has not 47 00:03:32,657 --> 00:03:39,393 yet started. Natural one to ten means countdown is 48 00:03:39,393 --> 00:03:52,990 running and how many seconds left. And complete means countdown is over. 49 00:03:52,990 --> 00:03:56,290 And if you want to, you can indulge your inner Virgo and make these line up 50 00:03:56,290 --> 00:04:01,099 nicely. And if you don't want to you don't have 51 00:04:01,099 --> 00:04:04,303 to. There's the type comment and the 52 00:04:04,303 --> 00:04:09,212 interpretation. Now we need some examples. 53 00:04:09,212 --> 00:04:14,228 So we might say, you know, define countdown one is now I'll put a false 54 00:04:14,228 --> 00:04:19,409 here. It's an atomic distinct so in some sense 55 00:04:19,409 --> 00:04:26,413 you might find that that example is not particularly illustrative but maybe I'll 56 00:04:26,413 --> 00:04:32,902 have countdown two is ten, and I could put a comma here if I want, just started 57 00:04:32,902 --> 00:04:41,727 running. Define countdown three, almost over and 58 00:04:41,727 --> 00:04:48,656 define countdown four complete and that one's clear. 59 00:04:48,656 --> 00:04:57,182 So there's my examples. Now we'll set up to do the template. 60 00:04:57,182 --> 00:05:04,142 Define fun for count down c. That'll be some body. 61 00:05:04,142 --> 00:05:15,203 Template rules used. So now we have to go find the first 62 00:05:15,203 --> 00:05:18,089 template rule used. And again, we always start by looking at 63 00:05:18,089 --> 00:05:22,215 the word right after is in the type comment, and that word is one of. 64 00:05:22,215 --> 00:05:27,957 So if we go find the template rules page, again we can find that by following this 65 00:05:27,957 --> 00:05:33,162 link. The beta driven templates recipe link. 66 00:05:33,162 --> 00:05:36,267 The one of rule is the rule we're going to use because this is an 67 00:05:36,267 --> 00:05:39,843 itemization. The one of rule is also what we use for 68 00:05:39,843 --> 00:05:42,850 numerations, so we know how it starts out. 69 00:05:44,190 --> 00:05:48,200 It's going to tell us to make a cond with one clause per sub class of the one of. 70 00:05:49,720 --> 00:05:53,560 So I'll go back over to the code now and I'll say cond and there's three sub 71 00:05:53,560 --> 00:06:02,826 classes. So there'll be three question answer 72 00:06:02,826 --> 00:06:08,680 pairs. And I'll say that the rule that I used 73 00:06:08,680 --> 00:06:12,650 was one of three cases. Okay, so now I go to the first case. 74 00:06:12,650 --> 00:06:20,863 The first case is false. Well, false is an atomic distinct value 75 00:06:20,863 --> 00:06:29,370 and I need a predicate for the question that tests for false. 76 00:06:29,370 --> 00:06:32,115 And it turns out there is a predicate that tests whether a value is exactly 77 00:06:32,115 --> 00:06:37,072 false. So back at the code I could put that 78 00:06:37,072 --> 00:06:43,662 predicate in, I could say false?c. This is atomic distinct so dot dot dot. 79 00:06:43,662 --> 00:06:56,092 Atomic distinct false. So now for the second subcase, let's see, 80 00:06:56,092 --> 00:07:00,716 this is an interval, so going back to the data templates page, for interval, I need 81 00:07:00,716 --> 00:07:07,600 an appropriate predicate. So, this is a predicate, it for example 82 00:07:07,600 --> 00:07:10,525 tells me is it a number between one and ten. 83 00:07:10,525 --> 00:07:16,625 But I have to be a little careful here. If I just make this predicate be and, 1 84 00:07:16,625 --> 00:07:22,230 is less than or equal to c, and, c is less than or equal to 10 and let me just 85 00:07:22,230 --> 00:07:31,098 do the answer clause quickly so we'll have that out of the way. 86 00:07:31,098 --> 00:07:36,857 Since this is an atomic non distinct this would be ...c. 87 00:07:36,857 --> 00:07:43,487 That might be what you write but this particular itemization is called a mixed 88 00:07:43,487 --> 00:07:48,814 data itemization. It's a mixed date itemization because 89 00:07:48,814 --> 00:07:53,925 there's different kinds of data in it, not all three clauses are numbers. 90 00:07:53,925 --> 00:07:57,780 The first clause of the data is a Boolean. 91 00:07:57,780 --> 00:08:01,640 The second clause, it's a number, in the third it's a string. 92 00:08:01,640 --> 00:08:05,932 So I have to be very careful here, because if I call this template with c 93 00:08:05,932 --> 00:08:10,816 being the string complete, this less than or equal right here, this less than or 94 00:08:10,816 --> 00:08:17,700 equal is going to blow up. Because you can't call less than or equal 95 00:08:17,700 --> 00:08:24,222 giving it a string as an argument. So because this is a mixed-data 96 00:08:24,222 --> 00:08:29,716 itemization, we have to guard this less than or equal against being called with a 97 00:08:29,716 --> 00:08:35,465 value that isn't a number. And the way we're going to guard it is 98 00:08:35,465 --> 00:08:39,485 we're going to say well you're only this middle case if you're a number and you're 99 00:08:39,485 --> 00:08:49,694 a number between one and ten. So I'm going to add one more test to the 100 00:08:49,694 --> 00:08:55,137 and. So now if I call a fun for countdown with 101 00:08:55,137 --> 00:09:00,099 false, this template will go there. If I call fun for countdown with a number 102 00:09:00,099 --> 00:09:03,660 that is between one and ten, this template will go there. 103 00:09:04,810 --> 00:09:10,018 So now I've done the code properly, and I need to add the template rule that I 104 00:09:10,018 --> 00:09:19,316 used. atomic non-distinct, and it's this 105 00:09:19,316 --> 00:09:28,920 interval right here. I'll just copy it. 106 00:09:28,920 --> 00:09:32,030 Now going back over to the data templates page. 107 00:09:32,030 --> 00:09:34,814 There's a note here that says it is permissible to use else for the last 108 00:09:34,814 --> 00:09:37,980 question for itemizations and large enumerations. 109 00:09:37,980 --> 00:09:42,476 This is an itemization. So, we're allowed to use else for the 110 00:09:42,476 --> 00:09:46,948 last question. What that means is, right here for this 111 00:09:46,948 --> 00:09:52,407 question, I'm allowed to put else. I don't have to put a question that tests 112 00:09:52,407 --> 00:09:56,180 whether c is actually the string complete. 113 00:09:56,180 --> 00:10:00,169 Let me do the answer quickly. This complete is an atomic distinct, so 114 00:10:00,169 --> 00:10:04,578 the answer's going to be that. Now let me talk about why I'm allowed to 115 00:10:04,578 --> 00:10:07,054 put this else here, and it's really important. 116 00:10:07,054 --> 00:10:12,400 In this course, if you have written a well-formed type comment like countdown, 117 00:10:12,400 --> 00:10:17,341 and you later say that a function consumes a countdown, then you can count 118 00:10:17,341 --> 00:10:23,920 on the function being called with a legal countdown. 119 00:10:23,920 --> 00:10:29,156 And so that what that means is, when this template runs in some specific function, 120 00:10:29,156 --> 00:10:34,315 if c isn't false and c isn't a number between one and ten,.then c is guaranteed 121 00:10:34,315 --> 00:10:39,980 to be the string complete. You don't have to actually test here 122 00:10:39,980 --> 00:10:45,216 whether c is the string complete. What we're saying is that having taken 123 00:10:45,216 --> 00:10:48,714 the trouble to do the type comment and having taken trouble to write, to specify 124 00:10:48,714 --> 00:10:54,850 the signature of a function. You can count on that being respected. 125 00:10:54,850 --> 00:10:57,301 The reason that's a reasonable thing to do in this course, is in other 126 00:10:57,301 --> 00:11:00,010 programming languages that you will use, there's a part of the programming 127 00:11:00,010 --> 00:11:03,440 language implementation called the compiler. 128 00:11:03,440 --> 00:11:08,220 Which will actually enforce that rule to make sure that it's always true. 129 00:11:08,220 --> 00:11:10,870 So it's a reasonable rule for you to start counting on here. 130 00:11:10,870 --> 00:11:17,074 So now that I've done this last case. I do need to go and add my template we'll 131 00:11:17,074 --> 00:11:25,076 use, which is atomic distinct and it's this complete here. 132 00:11:25,076 --> 00:11:28,905 I'll copy it. So, let's see, I'll run it to make sure 133 00:11:28,905 --> 00:11:32,898 everything's well formed. I don't get any errors so it is well 134 00:11:32,898 --> 00:11:37,815 formed and I will comment out that piece of it. 135 00:11:37,815 --> 00:11:43,860 Save the file, and now I've got the data definition for this countdown type.