Lumaktaw sa pangunahing content

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


  1. Signature, purpose and stub
  2. Define examples, Wrap each in check-expert (for racket only, use assertion tests appropriate for your language)
  3. Template and Inventory
  4. Code the function body
  5. 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 a number and outputs a number.

;Signature
;Number -> Number

Then we list the purpose of the function, it is already stated in the requirements.

;Produces 2 times the given number

Then finally we have the function stub. It is a piece of code that bootstraps or scaffolds the function to give some expected output of the correct datatype regardless of the input. It is to prepare the function to be written.

;a stub - Racket
(define (double n) 0)

You can design stubs in your preferred programming language that supports functions/procedures/definitions/subroutines or whatever the language specifications call it.

The next step which is examples, allows us to write test cases where we would check if the function produces the expected output. For Dr. Racket, there is a primitive called check-expect Which is racket's assertion test library. Programming languages have their own test frameworks or assertion libraries.
  
(check-expect (double 3) 6)
(check-expect (double 4.2) 8.4)

Both of these tests are obviously gonna fail as the stub has not been fully defined yet. It allows problems to be detected early on

check-expect: actual value 0 differs from 6, the expected value.
check-expect: actual value 0 differs from 42/5, the expected value. The template makes the clear sense of what the function has to work with. It identifies on how the parameter should work with the function, for primitives, it can be as simple as an ellipse and that parameter (define (double n) (... n)) This tells us that we will do something with the parameter n, we will write more complex templates for complex functions Then we start coding. Using the previous steps that we undertook, you now should have an idea on how to implement this. To have an idea on what the code should be, refer to your test cases and the purpose to learn how the expected output is made. For this example, the result is due to the multiplication process, and we have clues in the purpose on why is that so. Then, you can implement multiplication in your function body, using the evaluation rules to get the logic right. These is how the previous process helps with the function design 
  • the signature tells you the type of the parameter(s) and the type of the data the function body must produce
    • the purpose describes what the function body must produce in English
  • the examples provide several concrete examples of what the function body must produce

  • the template tells you the raw material you have to work with

    The last step is to test and debug.

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