Lumaktaw sa pangunahing content

Mga Post

EP: 13 - Interval Data Definitions

Part 13. Interval Data Definition We use intervals to define information where it has a specific range. Here is the Example Problem: Imagine that you will make a data definition for the seat number in a row on a certain theater. There are 32 seats in a row. If you have been in some arena or gymnasium. You know that the seats are horizontal in nature. There are seat numbers from 1 to 32. That is the information that we can get from the problem domain. In our type definition, we declare the seat number as SeatNum and it is an integer between 1 and 32. Square brackets are inclusive and parenthesis are exclusive. Just like the number line in elementary math. Next, the interpretation of the data.   We need to be specific with our interpretations of the data. We have to determine what these kinds of seats are. Now for the examples Finally, the template, we derive the template from the HtDD recipe. We also need to specify what data we used. And we are done wi...

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 A possible  structure definition  (not until compound data) A  type comment  that defines a new type name and describes how to form data of that type. An  interpretation  that describes the correspondence between information and data. One or more  examples  of the data. A  template  for a 1 argument function operating on data of this type. We need to deci...

EP: 11 - cond Expressions

Part 11. cond Expressions There are some cases where we need to have 3 or more parallel conditions. Unlike most languages, racket, a lisp dialect, has only 2 expressions for an if-expression.  This is solved using an elseif or a switch, the racket equivalent is the cond expression. Here is an example. We can use cond to have multiple questions without relying on if-expressions. We can also use square brackets but parenthesis also works. A cond has a question and answer pair. The last question can use else in place of the question. Evaluation rules First, the question and answer pairs are evaluated each. The question is then evaluated which outputs a boolean. If true, then the entire cond expression is then replaced with the answer. If false, the pair is dropped.

EP: 10 - Poorly formed problems

EP 10. Poorly formed problems Sometimes, poor requirements analysis and vague problems can cause miscommunication and forces the programmer to make an assumption, which is not always good.  Take a look at this problem Design a function that takes the image and determine whether the image is tall. Now, this is somewhat problematic, we need clarification on what makes an image tall. Let's try to do the HtDF methodology. The signature would be ;; Image -> Boolean Since this seems to be a yes or no question, we are going to output a boolean. The purpose of this function would be to produce true if the image is tall. When designing the purpose with the boolean as the output, you must put there on what makes the condition true. It specifies on how to interpret the output. Now we produce the stub of the function, We need to know what is the name of the function, which is not specified by the problem. You will have to invent the name of a functio...

EP: 9 - What not to do when designing functions

Part 9. Errors when designing functions. Mistakes can be made when designing functions. Here are common mistakes when using the HtDF method. 1. Do not repeat the Signature when making the purpose. The signature only shows us the Input and Output of a function, it does not say anything about its purpose. The purpose asks the question on what we do with the parameters and how can it generate the output. 2. Make sure the test is consistent with the purpose, should the test comes up wrong, either check the test itself with the purpose, signature or check the function body. Tests can also fail, take a deep hard look on what is causing that problem, there is a chance that the test is wrong so check it also.

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