Lumaktaw sa pangunahing content

EP: 12 - Data design

EP. 12 - Data design

While designing functions is a necessary skill, we also need to design data. The design of our data will affect on how our functions are implemented.  

Here is an example problem, We need to make a data definition to represent the name of a city.

The first thing we do is that we need to form the information that we are trying to represent.

Let's have the sample data first, for examples, we are going to list Manila and Quezon as part of the information. These are all Informations.

Making data definitions has 5 basic steps

  1. A possible structure definition (not until compound data)
  2. type comment that defines a new type name and describes how to form data of that type.
  3. An interpretation that describes the correspondence between information and data.
  4. One or more examples of the data.
  5. template for a 1 argument function operating on data of this type.
We need to decide on what kind of data definition to use, based on the form of information we are trying to represent. 

There are 7 Data definitions that we can choose.

  1. Simple atomic data
  2. Interval
  3. Enumeration
  4. Itemization
  5. Compound data
  6. References to other defined type
  7. Self referential or Mutually referential
Since city names are already considered atomic data, which means no information cannot be derived anymore from them and are considered basic data. We can use Simple Atomic Data as our data definition.

Data definitions have their own template. Regarding the names of cities, we can declare a variable for the data and decide what datatype should the information represent to. For this case, since city names compose of sequences of characters, we can use strings to represent cities.

We also need to interpret the data to know what kind of information is it. We need to specify what is the meaning of the Data that we would use

Examples of the data are also needed, in this case, 2 examples are enough for Simple Atomic Data.

The next step is to produce the template. A data driven template is a function that will serve as a guide on how to approach that data.

A data driven template generally looks like this

(define (fn-for-type-name x)
  <body>)

We now need to pick a template rule. A rule of thumb is to look at the datatype representing that information. Since the datatype specified here is string. We can use the template for the string.


We have completed the data definition, now we just need to create the function. This is how we use HtDF in conjunction with HtDD.

For this example, We need to have a function that determines if the entered city is the best city in the world. That function will consume the data that we have declared.

We can follow the steps of HtDF

First, the signature

Since we are asking if the entered city is the best city. We would use a boolean datatype.

The purpose would be to produce a true value if the given city is the best city in the world, remember about being specific when dealing with booleans.

Let's make the function stub

Now we can make the test cases. Since we are working with booleans, we need to have 2 cases in which covers both results.

Now to run the tests to make sure my stub is correct.
Even if the results failed, the two test ran and that is what matters at this point.

We can take the template from our CityName Definition since our function takes it as an input anyway. Note that we need to specify which template we took it from.

We should have an Idea on what code should this function contain. We need to compare strings since it is asking when the given string is the best city.

Now we have a working function that satisfies the problem domain

Mga Komento

Mga sikat na post sa blog na ito

Problem Solving using HtDF method

Problem Solving In this problem, we will use the HtDF method to create a function. Problem:  Design a function that pluralizes a given word. (Pluralize means to convert the word to its plural form.) For simplicity you may assume that just adding s is enough to pluralize a word. Here is the problem that we need to convert into a function. We need to do it systematically using the HtDF method. First we need the signature, recall that the signature will tell us what is the input and expected output. A word is the input and output of the function, and we can represent words, which is just a sequence of characters if you think about it, like a String. So the signature of the function is ;;String -> String Then we will need to know what is the function's purpose, we can look at the specifications to know the purpose. It says that the word is pluralized in the process "assumption that adding s at the end is the pluralized form of the word". Ther...

EP: 18 - HtDF with Itemization

Part 18. HtDF with Itemization From the Countdown Display definition that we have made earlier, we are going to make a function out of it. We have to output an image depending on the state of the countdown.  Here is the Signature, Purpose and Stub. It is recommended to run the code often, the earlier we detect bugs, the better. We need some examples. For tests on itemizations, we need to have as many tests as there are cases. Also, if we have an interval, we have to test points of variance. For the Interval, there is not much variance in the seconds before midnight so it is okay to have only one test for that. There is a primitive called text which converts text to an image, we can also use number->string to convert a number into a string datatype. Note that when we are devising tests, we are already working out the behavior of the function before we start coding. Now we get the template and start creating the function body.  ...

EP: 19 - big-bang Mechanism

Part 19. big-bang We are using Dr. Racket to design interactive programs, from this day forward, this is going to be very complicated. Imagine we have 2 interactive programs, one that counts down from 10 to 0 and some animation of a cat walking in the screen. These programs are interactive by just pressing the space key, the program restarts. These interactive programs change state and it affects the program behavior. On the countdown program, it generates a number from 10 to 0 every 1 second. It is also the same with the cat program, it just changes the x coordinate of the cat every miliseconds. These are 2 different functions, now how can we combine these 2 functions. We combine them by using Dr. Rackets big-bang primitive. (big-bang 0           (on-tick next-cat)           (to-draw render-cat)) We use 0 as the initial world state, then on every tick, we get the next x coordinate, we pass it to the render c...