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. A 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. A 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

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.  ...

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: 7 - Stepper, Dr Rackets Debugging tool

Part 7. Stepper Normally, Programmers use debugging tools to detect errors early on, One such tool in Dr. Racket is the stepper tool, the stepper tool is used to evaluate every step of the program to show the programmer what is happening in the execution of the code. This is an example code. As we write more complicated programs, we will need the use of the stepper to check the code during runtime. The stepper is launched by pressing the step button beside the run. When the stepper is launched, a new window opens, one by one, the code is evaluated. The green color means that the expression has to be evaluated, the output of that is placed in color purple. After the forward step is pressed, the right expression goes to the left and it is then evaluated The next line is then evaluated, A function declaration does not produce any value but the parameter of the function which is an image is evaluated. When the parameters of the function is eva...