Now we're going to work through an example of designing a data definition from
scratch using the How to Design Data
recipe and the accompanying Data-Driven Templates recipe.
It's going to be a simpler example than the
traffic light example you saw earlier in the week.
What we're going to do over the next couple of lessons is
build up our skills to desgining more complicated data definitions like that.
What
I've got is the city name starter from the week two page.
Is says imagine that I designed a program that, among other
things, has information about the names of cities in its probability.
Design a data definition to represent the name of the city.
So
the key first thing that we do when we design a data definition
is to work out the form of the information they're trying to represent.
And it might even be good to just write down a couple examples.
Maybe I'll put a little Comment Box right here.
And I'll write some examples. So one example city name is Vancouver.
And another example city name, we have
Boston for example, and these are all information.
And
now the question is, how am I going to represent that?
What I'm going to do is go to the court's
website and I'm going to go to the design recipes page.
And this again provides me an index into all the design recipes.
And I'll go to the How to Design Data Recipe.
In this web page I won't go through in detail now, I'm just
going to work through it by example but you should review it on your own.
It tells me about the basic parts of every data definition.
And I'll pick this up later and set it
down off to the right when we're working in Dr.
[UNKNOWN].
And the key thing it helps me do, is decide what kind of data
definition to use Based on the form of the information that we need to represent.
Now remember we're trying to represent
city names things like Vancouver and Boston.
And the thing about that information is that it's atomic And what I mean by
atomic is, you can't take it apart into pieces that are still meaningfully part of
the same problem domain. So yes, it's true.
I can take the city name Boston apart into the letters b o s It's TON.
But those aren't meaningfully part of a city name, in a
sense, they're not like neighborhoods of the city or something like that.
So this information is atomic, and the way we represent atomic information
is with a type comment that looks like this.
We simply have a new type name,
and we say what underlying primitive atomic data it's going to represent it as.
So going back over here to DrRacket, I'm going to say,
Cityname is String. A good way to represent this information.
He's using strings, so for example, if you do this the data that represents the
city name Vancouver is going to be the string Vancouver.
And The data that represents the city name Boston is going to be the string Boston.
So here's my type comments.
So the name is string and now I need
to provide interpretations so I type interpret and for
[UNKNOWN]
atomic data the data interpretation is often fairly simple, especially for a
case like this We're just going to say the name of a city.
Now, the next part of the data definition is something that
we didn't see in the last example, and we'll talk about
why that is more in a couple videos, but we want
to provide some examples of how the data definition represents information.
So what I'm going to do is I'm going to say
Define city name one is, well one good example is Boston.
And define city name two, and another good example is
Vancouver. Now, I'll be honest with you.
Providing examples of really simple data definitions like
this doesn't do that much work, but again
we're learning the recipe now so that it
will help us more with more complicated examples.
And fairly soon we'll have data definitions Where
the example's really are going to help us understand the data definitions better.
So there's the examples.
The next step of the recipe is to produce the template for this data definition.
Now to produce the template, what I do, I go back to the course website,
and I go to the design recipes page, and I get the data driven templates rules.
So here's the data driven templates recipe.
And the way it works is it says for a
given type name, the data driven template looks like this.
So
what I do is I say define fun for city name
and we're going to be quite particular about the name of the template.
Naming the template directly after the type name is going to turn
out to help us Quite a bit later on in the course.
And I'll pick as a parameter name for this template, CN.
And now I've gotta do something about the body here.
And what I'm also going to do is
I'm going to write template rules used, like this.
And now looking back at the data-driven templates page, What I see is
the way to produce the body is to follow the rules in this table.
What I do is go to the type comment.
I look at the first word after is, so that's string.
And then I start working in this table
from there.
Now string is what's called an atomic non-distinct type.
The best way to understand what non distinct means is to wait
for a couple more videos and then we'll see what distinct examples are.
But string is right here, so this is the row of the table I'm going to use.
And I'm not inside of a cond, you'll see what it
means to be inside a cond again in a couple of videos.
But for the body, I'm just going to say dot
dot dot x where x is the actual parameter name.
So the body of this template is just going to be ...
cn.
And I'm also going to write here, the template rule that I used, and
the rule that I used was the atomic non-distinct rule.
[SOUND]
And I'm also going to write the part of the type
comment that matched that rule, which in this case is string.
And now I'm going to run this and there's nothing really to
run but the key thing is I didn't get any errors.
If for example I had a mistake like I left a
parenthesis off there then I would get an error right now.
And I would fix this poorly formed template right now.
So I'll fix this poorly formed template right now.
And now I've got this running properly.
And what I'll do is I'll comment out the
template, because we're never actually going to call this template function.
We're just going to copy it into later function design.
And I'll get rid of this now, this scratch work.
Because that's already really captured in the data definition.
So there you go, I've completed a first data definition by following the how to
design data recipe using also the data driven templates recipe.
In the next video we will define a function that uses this data definition.