Data Types Revision

One type, two type, red type, blue type.

Abstract type, algebraic type, record type, product type.

This one has a definition. This one has some repetition.

Say! What a lot of types there are.

-Tony “The Cat in the Hat” Hosking

Here are some good revision questions for data types. They are fairly open ended, to get you thinking about how you may implement some potential programs.

Question 1.

What is an abstract data type? What is an algebraic data type? Can data types be recursive? If so, should they be primitive recursive? Why, or why not? Create a recursive data type.

Question 2

Define a suitable data type for a set of points on a 2D grid. Now define another, different one.

Question 3

Define a data type for a trinary tree (three branches from each node).

Question 4

At Jay’s Pizza Parlour, pizza’s are served hot with a side of data types. Like at Domino’s, you can order a Pizza online by providing your name. You can order a pizza with one of three primary toppings: Chicken, Veggies and Jay’s Mystery Meat™ . Additionally, you can have up to two choices of secondary toppings: Cheese, jive turkey, pepperoni, icecream, and tofu. Each pizza also has an associated cost. Your pizza is ready when Jay says so, and a light on a screen will go green next to your name so you know that this is the case. Otherwise, the light remains red.

a) Develop a data type, Order that encapsulates all of these elements for ordering a pizza: Name, primary topping, secondary topping, cost, and whether your pizza is ready yet. Draw a diagram if this helps. Remember you can have up to two secondary topings.

b) Is your Order type abstract or algebraic?

c) Develop a data type to handle multiple Orders (perhaps a list?). Is this type abstract or algebraic?

d) Jay wants money now! Let him prioritise his most expensive pizzas first. Write a function that organises a list of Orders based on their price, arranging them in descending order.

e) Write a function that adds up all the costs of a set of Orders, so Jay can calculate his final profit.

f) Order up! Jay wants the names of the people who’s pizzas are ready. Given a set of Orders, write a function which extracts the names of the people whose pizzas are ready.

g) Uh-oh! It seems the health inspector is visiting tonight, and for this reason Jay does not want to sell any pizzas with Jay’s Mystery Meat™. Write a function that deletes all pizzas with Jay’s Mystery Meat as a primary topping from a set of Orders.