Here is another how to design data problem.
In this case, it's going to be for an enumeration.
So, the data definition that we produce will be similar to the traffic light
color example from earlier in the week. This is the problem in
letter-grade-starter.rkt, and again I know this is a data design problem.
So, I've got the how to design data recipe here and I've got the how to
design data web page open in my browser here.
We're designing a system to keep track of student grades.
And we need to design a data definition to represent the letter grade in a course
which is one of A, B, or C. The key thing about the information in
this problem is that there's three distinct values.
In other words, every letter grade is either going to be the distinct value A,
or the distinct value B, or the distinct value C.
It isn't like something where we might be keeping track of the numerical score on a
course, which is a whole bunch of numbers.
Here, there's really a modest number of distinct values A, B or C.
And when the problem domain information is like that.
When it consists of a fixed number of distinct values, then there's a special
kind of data definition to use for that called enumeration.
In the way enumeration works is that we're going to use one of, in the type
comment, this way. Now, this is exactly like the traffic
light data definition from earlier this week.
So, here we go. I'm going to say, LetterGrade is one of.
And now, what I'm going to do is I'm going to talk about the data used to
represent each of the distinct cases. And I'll use strings in this case, I'll
say that these are the three distinct data values which are going to represent
the three distinct information values. And when you do an enumeration, the
interpretation tends to be relatively straightforward.
In this case, you'll just say the letter grade in a course.
But if you think back to that example we saw at the beginning of the week.
If, for example, you have decided to use the three data values, maybe numbers like
0, 1, and 2, well then, your interpretation would be more substantive.
Then your interpretation would have to say something like, you know, 0 means A,
1 means B, 2 means C. So, part of why the interpenetration is
simple in this case is because we're using strings to represent the three
cases. And the strings will clearly say what the
cases are. The other thing that happens in
enumerations is that examples are kind of silly.
I mean, if we tried to write examples for this, what would we write?
We'd write something like define letter grade 1 as A means A.
Define letter grade 2 as B means B. Define letter grade 3 as C means, means
C. It's kind of silly, right?
Its ridiculous. Because it's an enumeration there's only
three values. We know what the examples are before we
get to the example stage. So, we don't even bother to write them.
What we write here is we say examples are redundant for enumerations.
And once we get a couple weeks farther in the course, you don't even have to write
that. We want you to write it for now, just as
a way of remembering that the example step existed.
Now, we're going to do the template. So, define fn-for-letter-grade, grade,
lg. There's going to be some body here,
template rules used. And here we go.
I'll go back over to the How to Design Data web page, here we were.
And I'll scroll up to find a link to the Data Driven Templates recipe.
And, here we go. So, I need to look in this column for the
first word after is. Now remember, the first word after is is
1 of. So, it's not any of these, there's a
special rule for one of. And one ofs are always in numerations
like this case is. One of itemization which is the example
we are doing after this. And now, this says that the body is a
cond with one clause per subclass of the one of.
What does that mean? What's the subclass of the one of?
What's going on here in this one of is this three cases or three sub-classes.
This says every LetterGrade in the world is either, in this class, in this class,
or in this class. Every LetterGrade in the world is either
an A, a B, or a C. So, here we go, it says that we're
going to make a cond with one clause per sub-class of the one of, so let me just
do that. I'm going to say cond, and there's
going to be three of these, because there's three there, so there'll be three
here. And down here in my Template rules, I'm
going to put that we used the one of rule and that there are 3 cases.
Now, what happens, well now, I've gotten past the one of.
And the next thing I see is an A. The next thing I see in fact, in fact, is
the string A. So, which template rule is that?
Because what this says is that, for each question and answer expression, I'm
going to form it by following the rule in the question or answer column of this
table for the corresponding case. So, I'm going to this A, and I need to
find its row in this table, and A is an atomic distinct value.
Now, you understand what the distinct value rule that we were just skipping
over before was. There's a single value, the string A.
So, it's this case. And now, you also understand what the
conned question column of this table was for.
It says that in the question column, we're going to put the appropriate
predicate. Which is a string equal in this case.
So, we'll go back over here and we'll say string=?
letter grade to A. That's the question.
And in the cond answer, we're going to put just dot dot dot.
Now, you might ask why aren't we putting dot dot dot letter grade.
Well, two answers. One answer is the table, is the table
told us to put just dot dot dot. It didn't tell us to put dot dot dot and
the name of the parameter. It told us to just put dot dot dot, but
the question is why does the table say that?
Well, the table says that because in this case we know what lg is.
lg is the string A. It'll always be the string A if we get to
this cond answer. So, we're not putting it there to make
that more clear. And so, the template rule that we used
here was atomic distinct value A, now we go on to the next row.
Well, the next row is the string B, so we could go back to the table.
Let's see the string B is an atomic distinct value, so the cond question will
be an appropriate predicate. And the cond answer will be open
parenthesis dot dot dot close round bracket.
So, let's see the cond question. Is string=?, letter grade to B.
Then, the cond answer is dot, dot, dot. And this is atomic, distinct value B.
And at this point you can see how this is working and you can short circuit the
process of doing C a bit if you want. We'll just copy the case for B.
We'll get rid of this last question and answer pair.
We'll make that to C, and we can do that down here in the template rules as well.
Now, we'll run it. There's no errors.
So, it's all well formed. We'll comment that out.
So, this is an enumeration because the domain information here has two or more
distinct values A, B and C. And an enumeration data definition uses a
one of with the data values that are going to be used to represent the
information. And now we've seen two new template
rules, the one on rule and the distinct value rule.
So, there we go. That's our data definition for letter grade.