Lumaktaw sa pangunahing content

Mga Post

Ipinapakita ang mga post mula sa Pebrero, 2018

EP:20 - Domain Analysis

Part 20. Domain Analysis For interactive programs such as these, we will use the How to Design Worlds recipe. This is our guide for developing most interactive programs, in big-bang, these are called world programs. There are 2 parts when designing interactive programs, first is that we use pen and paper for the concept and then we then code the whole thing from our analysis. We are going to design the walking cat program using the recipe. The cat would start at the left edge of the display and walks across the screen. When it reaches the right side, it should just continue walking off screen The interactive part is that we can use the spacebar key to bring the cat back to the left side. There are 4 steps in domain analysis Sketch program scenarios Identify constant information Identify Changing information Identify big-bang options First we sketch program scenarios, we would need pen and paper for this one. These 2 or 3 images shows the states of t...

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

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: 17 - HtDF with Enumeration

Part 17. HTDF with Enumeration In this example, We will be devising a function that gets the grade and bumps it to the next highest grade. Interesting thing about data definitions is that you do not have to make a data definition for each function. A data definition for that set of data can be reusable for any function that takes it as an input. Here we will define the signature, purpose and stub. Note that there is no grade above A. We will have to decide what happens to it. For now, there would be no change if we hit the highest grade. We need some examples. In enumerations, we should at least have as many tests as the total number of cases. We have 3 cases as defined by the definition. So we should have 3 tests. We can copy the template from the definition. This makes it easier. Note. The template must not contain any errors, this will affect our code when we implement them. Do not forget to change the template name to the actual function name ...

EP: 16 - HtDF Interval Data Definition

Part 16. HtDF with Interval Data Definition We have defined the data definition for an interval, now we will make the function using HtDF recipe  The example asks us to make a function that produces true if the given seat number is an aisle For reference, look at the interpretation of data definition, it seems that seats 1 and 32 are aisle seats. Here we make the Signature, Purpose and Stub. Now we make the tests, When making tests for Intervals, include the first and last value and also the midpoints. Include also test for other points of variance across the interval.    Thanks to the Data definition provided, this helped us to create our test. HtDD and HtDF are related which can help us to design our functions. And finally, we just copy the template from the Data Definition, make sure you point that out. Comment out the stub and create the code. The function fulfills the requirements in the problem domain. Data definitions can help us define ...

EP: 15 - Itemization Data Definition

Part 15. Itemization Data Definition The Itemization Data Definition is what we use for information compromised of 2 different categories, at least one of each is not a distinct value. This data definition is quite complex. As an example: Design a data definition for the current state of the countdown for the display, which falls in one of 3 categories. Not yet started from 10 to 1 seconds until midnight Complete This is like the countdown clock in TV. By looking at these states, the second one is not a distinct data value. Therefore this is not an enumeration. In this case, we will use the Itemization Data Definition. We have to choose a datatype that fulfills the categories. For someone that has done strict programming style method for a long time now, Data consisting of multiple data types confuse my brain, but I think Lisp Based languages tend to be liberal about the types of parameters unlike the C family or it's influences. This is one of the thing that lisp ...

EP: 14 - Enumeration Data Definitions

EP. 14 - Enumeration Data Definitions Enumeration data definitions are used to represent information that has 2 or more distinct values. For example, We are designing a data definition for student grades. The grade can be A, B or C. In this problem domain, the grade can have 3 different values, which is A, B or C. Since we have a data which can have 3 distinct values, it makes sense that we use Enumeration for our template to make our data definitions. When making the Type Comment of the Enumeration Data Definition. We need to enumerate each distinct value.  When interpreting enumeration, this tends to be straightforward.  On enumerations, we do not need to have examples since they are redundant. The examples are already in our type comment. For the enumeration template, we need to make an if-conditions that would cover all the distinct values that we have here, in a Condition and the Corresponding expression.  Since the individual distinct value n...

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

EP: 8 - Function Design

Part 8. Function Design - HTDF Recipes The HTDF recipe that MIT uses to teach students to design functions is a perfect way to design hard functions easier, but the price to pay is that simple functions will have a hard time for designing using this method. The HTDF recipe model, when summarized, consists of 5 steps Signature, purpose and stub Define examples, Wrap each in check-expert (for racket only, use assertion tests appropriate for your language) Template and Inventory Code the function body Test and Debug until correct The first step is to define the Signature, Its purpose and the stub Here is an Example Design a function that consumes a number and reproduces twice that number. Call your function Double. Follow the HTDF Recipe and show the stub and template Let us do the first step. A signature is more of a data flow of a function. It requests the input type and produces the expected output data type of a function. In this example, the function must accept...

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