In this how to design data problem, the type comment is going to end up with the
one of. But not all of the subclasses are going
to be single distinct values. So this is what's going to be called an
itemization. And it'll be the most complex form of
data that we see this week. This is the problem in countdown starter.
So what we're supposed to do here is design a data definition to represent the
current state of a New Year's Eve countdown.
So this is something like the countdown ball in Times Square.
And we're asked to design a data definition for that.
As always, when we design a data definition, the first thing is to clearly
identify the information in the problem domain that we're trying to represent.
And in this case, the problem statement makes that very clear to us.
In particular, it makes it clear that the information falls into one of three
categories. The count down hasn't started.
It's from ten to one seconds before midnight or it's complete.
The previous problem we saw, the letter grade problem, also had three categories
and here the categories are different. This first one is kind of distinct.
The count down hasn't started. That's a clear, distinct state.
But in this second category, there's not a single distinct value there.
There's 10, 9, 8, all the way to 1. So there's a number of different values
there. And then in the third category, again,
there's a distinct state there. The countdown is complete.
So this isn't going to be an enumeration. This is going to be what's called an
itemization because there are two or more sub classes, at least one of which is not
a single distinct value. This center one is not a single distinct
value, so that's going to be an itemization.
And again after this video is over I encourage you to go to the Design Recipes
page, go to the How to Design Data page. This table will remind you that
itemization is what you need to use in a case like this and you can go and read
about the itemizations. Do that carefully after this video is
over. I'll work through the problem now.
So let's see. We start with the type comment as always,
so we'll say that countdown, is one of. And there's three subclasses.
And now we have to decide for each subclass how we'll represent that, what
data will represent that. Well let's see, if the countdown hasn't
yet started that's kind of a distinct state.
So we could use any piece of atomic distinct data to represent that.
Let's just use false. Then we will use the Boolean value false.
To represent that the countdown hasn't yet started.
Now, in the second subclass, let's just say that that'll be a natural number from
one to ten, inclusive. In the third case, we also need a
distinct value there and we'll just use the string, in this case, complete.
So now, we've got three distinct sub classes that correspond to the three
distinct kinds of information that need to be represented.
Well, we'll do an interpretation here, and I'll use multiple lines for this
interpretation. I'll say false means countdown has not
yet started. Natural one to ten means countdown is
running and how many seconds left. And complete means countdown is over.
And if you want to, you can indulge your inner Virgo and make these line up
nicely. And if you don't want to you don't have
to. There's the type comment and the
interpretation. Now we need some examples.
So we might say, you know, define countdown one is now I'll put a false
here. It's an atomic distinct so in some sense
you might find that that example is not particularly illustrative but maybe I'll
have countdown two is ten, and I could put a comma here if I want, just started
running. Define countdown three, almost over and
define countdown four complete and that one's clear.
So there's my examples. Now we'll set up to do the template.
Define fun for count down c. That'll be some body.
Template rules used. So now we have to go find the first
template rule used. And again, we always start by looking at
the word right after is in the type comment, and that word is one of.
So if we go find the template rules page, again we can find that by following this
link. The beta driven templates recipe link.
The one of rule is the rule we're going to use because this is an
itemization. The one of rule is also what we use for
numerations, so we know how it starts out.
It's going to tell us to make a cond with one clause per sub class of the one of.
So I'll go back over to the code now and I'll say cond and there's three sub
classes. So there'll be three question answer
pairs. And I'll say that the rule that I used
was one of three cases. Okay, so now I go to the first case.
The first case is false. Well, false is an atomic distinct value
and I need a predicate for the question that tests for false.
And it turns out there is a predicate that tests whether a value is exactly
false. So back at the code I could put that
predicate in, I could say false?c. This is atomic distinct so dot dot dot.
Atomic distinct false. So now for the second subcase, let's see,
this is an interval, so going back to the data templates page, for interval, I need
an appropriate predicate. So, this is a predicate, it for example
tells me is it a number between one and ten.
But I have to be a little careful here. If I just make this predicate be and, 1
is less than or equal to c, and, c is less than or equal to 10 and let me just
do the answer clause quickly so we'll have that out of the way.
Since this is an atomic non distinct this would be ...c.
That might be what you write but this particular itemization is called a mixed
data itemization. It's a mixed date itemization because
there's different kinds of data in it, not all three clauses are numbers.
The first clause of the data is a Boolean.
The second clause, it's a number, in the third it's a string.
So I have to be very careful here, because if I call this template with c
being the string complete, this less than or equal right here, this less than or
equal is going to blow up. Because you can't call less than or equal
giving it a string as an argument. So because this is a mixed-data
itemization, we have to guard this less than or equal against being called with a
value that isn't a number. And the way we're going to guard it is
we're going to say well you're only this middle case if you're a number and you're
a number between one and ten. So I'm going to add one more test to the
and. So now if I call a fun for countdown with
false, this template will go there. If I call fun for countdown with a number
that is between one and ten, this template will go there.
So now I've done the code properly, and I need to add the template rule that I
used. atomic non-distinct, and it's this
interval right here. I'll just copy it.
Now going back over to the data templates page.
There's a note here that says it is permissible to use else for the last
question for itemizations and large enumerations.
This is an itemization. So, we're allowed to use else for the
last question. What that means is, right here for this
question, I'm allowed to put else. I don't have to put a question that tests
whether c is actually the string complete.
Let me do the answer quickly. This complete is an atomic distinct, so
the answer's going to be that. Now let me talk about why I'm allowed to
put this else here, and it's really important.
In this course, if you have written a well-formed type comment like countdown,
and you later say that a function consumes a countdown, then you can count
on the function being called with a legal countdown.
And so that what that means is, when this template runs in some specific function,
if c isn't false and c isn't a number between one and ten,.then c is guaranteed
to be the string complete. You don't have to actually test here
whether c is the string complete. What we're saying is that having taken
the trouble to do the type comment and having taken trouble to write, to specify
the signature of a function. You can count on that being respected.
The reason that's a reasonable thing to do in this course, is in other
programming languages that you will use, there's a part of the programming
language implementation called the compiler.
Which will actually enforce that rule to make sure that it's always true.
So it's a reasonable rule for you to start counting on here.
So now that I've done this last case. I do need to go and add my template we'll
use, which is atomic distinct and it's this complete here.
I'll copy it. So, let's see, I'll run it to make sure
everything's well formed. I don't get any errors so it is well
formed and I will comment out that piece of it.
Save the file, and now I've got the data definition for this countdown type.