8.7

Lab 9: The table method

Recall the table method. It is a technique for tackling step 5 of the design recipe (function definition) after you have already finished steps 1–4. In step 5 of the design recipe, to come up with the correct function definition, the questions to ask are always
  • What do we have, according to the template?

  • What do we want, according to the examples?

  • How do we turn what we have into what we want?

The table method helps you organize, try, and build answers to these questions.

The Beginning Student Tables tool is available online for your use.

Exercise 1. (from The table method) Use the table method to design a function ctof that converts a temperature in Celsius to a temperature in Fahrenheit.

Exercise 2. (from The table method) Use the table method to design a function combine-digits that combines two digits (each an integer between 0 and 9) into an integer between 0 and 99. The first given digit should become the first digit of the output (in other words, tens). The second given digit should become the second digit of the output (in other words, ones).

Exercise 3. (from Problem set 4: Composing functions) Pretend that every year has 365 days. Use the table method to finish designing the following function:
; A Year is a non-negative integer
 
; A Month is one of:
; - "January"
; - "February"
; - "March"
; - "April"
; - "May"
; - "June"
; - "July"
; - "August"
; - "September"
; - "October"
; - "November"
; - "December"
 
; A Day is an integer at least 1 but at most 31
 
; year-month-day->days : Year Month Day -> Number
; returns the number of days elapsed since January 1, 0
; given: 0 "January" 1     expect: 0
; given: 2017 "August" 28  expect: 736444
Your design of year-month-day->days must use month->days-in-year:
; month->days-in-year : Month -> Number
; returns the days elapsed in the year before the given month
; given: "January"    expect: 0
; given: "September"  expect: 243
So, you should begin with tables that look like this: You don’t need to design the helper month->days-in-year, but you probably need to write more examples for it.

Exercise 4. (from Lab 3: Multiple cases) Use the table method to design a function that takes a number and produces an image that is the yellow text "STAR" above "WARS", centered over a black background. The larger the number, the smaller the yellow text should be:
  • When the input is 0, the yellow text in the output should have size 200.

  • When the input is 99, the yellow text in the output should have size 2.

  • When the input is 100 or greater, the text should disappear.

So, you should begin with a table that looks like this: To zoom into an image thumbnail, click on it.

Exercise 5. (from Problem set 5: Enumerations and structures) Use the table method to design a function called format-address which, given an address, produces a string that you might write on a letter to that address. In other words, this function should consume an address data structure and produce a formatted string. The function number->string may be useful.

To start the table, add rows for a variety of input Addresses, and add columns for all the ingredients supplied by the template for processing a Address, like this: In the Beginning Student Tables tool, put your data definition for Address and structure definition for address in the “Definitions Area” at the top.

Exercise 6. (from Problem set 6: Unions and recursion) Use the table method to design a function salary that accepts a Job and returns its salary, which is the initial salary of the entry plus all the raises of the promotions.

To start the table, add rows for a variety of input Jobs, and add columns for all the ingredients supplied by the template for processing a Job, like this: In the Beginning Student Tables tool, put your data definition for Job and structure definitions for entry and promotion in the “Definitions Area” at the top.

Exercise 7. (from Problem set 6: Unions and recursion) Use the table method to design a function weight that takes a Mobile as input and computes its total weight.

To start the table, add rows for a variety of input Mobiles, and add columns for all the ingredients supplied by the template for processing a Mobile, like this: In the Beginning Student Tables tool, put the data definition for Mobile and structure definitions for leaf and weight in the “Definitions Area” at the top:
; A Mobile is one of:
; - (make-leaf Number)
; - (make-rod Mobile Number Number Mobile)
(define-struct leaf [weight])
(define-struct rod [lm ld rd rm])

Exercise 8. Use the table method to design a function sum that takes a ListOfNumbers as input and computes its sum.

To start the table, add rows for a variety of input ListsOfNumbers, and add columns for all the ingredients supplied by the template for processing a ListOfNumbers, like this:

Exercise 9. Use the table method to design a function add-10-to-all that takes a ListOfNumbers as input and adds 10 to every number in it to produce a new ListOfNumbers.

To start the table, add rows for a variety of input ListsOfNumbers, and add columns for all the ingredients supplied by the template for processing a ListOfNumbers, like this:

Exercise 10. (from Problem set 7: Lists) Use the table method to design a function remove-<=100 that takes a ListOfNumbers as input and removes every number less than or equal to 100 to produce a new ListOfNumbers.

To start the table, add rows for a variety of input ListsOfNumbers, and add columns for all the ingredients supplied by the template for processing a ListOfNumbers, like this:

Exercise 11. Use the table method to design a function remove-first-<=100 that takes a ListOfNumbers as input and removes just the first number less than or equal to 100, if any, to produce a new ListOfNumbers.

To start the table, add rows for a variety of input ListsOfNumbers, and add columns for all the ingredients supplied by the template for processing a ListOfNumbers, like this: