On this page:
2 Lists of items
3 Lists of numbers
8.6

Lab 7: Tables and lists

1 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:

2 Lists of items

Do this section in groups of at least 2 people.

Your food-truck business already depends on shared services such as roads and sanitation. It can also take advantage of built-in list structures to let customers place orders that consist of any number of items.

Exercise 9. Complete the following data definition for an Order, by write a new data definition for an Item:
; An Order is one of:
; - empty
; - (cons Item Order)
 
; An Item is ???
You can make Item any kind of data definition: it could be
  • an atomic thing like a Number,

  • a range like a Number between 6 and 12,

  • an enumeration like a TrafficLight,

  • a structure like a Person,

  • a union like a Rocket, or

  • a recursive union like a TrainOfWagons.

Exercise 10. Define at least 3 examples of Orders. Define as many examples as it takes to make use of every line of your data definitions.

Exercise 11. Write the template process-order for a function that processes an Order, as well as the other templates for processing the other data definitions you introduced.
  • Point out where the data definition for Order refers to the data definition for Item. Does your template process-order refer to your template process-item in the corresponding place? It should.

  • Point out where the data definition for Order refers to itself. Does your template process-order refer to itself in the corresponding place? It should.

Exercise 12. Feel free to do this exercise and the next exercise in parallel. However, it is very important that y’all use the same data definitions that y’all just wrote.

Design a function price-item that takes an Item and returns its price (a number).

Then, design a function price-order that takes an Order and returns its price (a number). For example, the price of empty should be 0. Guided by the template, the function price-order should use the function price-item and itself.

Exercise 13. Design a function draw-item that takes an Item and returns a crude image of the food.

Then, design a function draw-order that takes an Order and returns a crude image of the foods. For example, the drawing of empty should be a tray or bag or plate or glass with nothing. Guided by the template, the function draw-order should use the function draw-item and itself.

Exercise 14. Go visit another group and try out their menu:
  1. Read their data definition carefully, but don’t bother reading the rest of their code.

  2. Make an Order. Make sure to obey their data definition for what an Order is.

  3. Give your order to the draw-order function in their Interactions Window. Is it appetizing?

  4. Give the same order to the price-order function in their Interactions Window. Is it worth it?

3 Lists of numbers

This section is optional. It will give you a head start on Problem set 7: Lists.

Exercise 15. 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 16. 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 17. 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: