ST EK List:
No EK's found

Creating the Contact ADT

Ruthless Suggestions from Al

Purpose (do/learn, why this/why here): Give students a programmatic experience of data as they engage in advanced list manipulations including the higher order function map.

In this lab, you will develop a Contact List database application like the one on your phone.

On this page, you will develop an Abstract Data Type to store and access contact data (name, address, phone number, etc.).

There are notes from BH here that are commented out.

There is user feedback here that is commented out.

Setting Up the Contact List

  1. Click here to load this file. Then save it to your Snap! account.
  2. Create a global contact list variable.
  3. Initialize contact list as an empty list.

Why global?

Also, in Snap!, only global variables save their data when the project is saved.
Multiple scripts across the project will use this variable, so it shouldn't be attached to just one script like a local, script variable.

Creating the ADT

Betsy and Gamal are planning to build a Contact List in Snap!.
Betsy: We need a way to add a contact to our contact list.
Gamal: Each contact will be a list that with the a person's name, phone number, address, email, or whatever we write.
Betsy: In Unit 2, we created a point abstract data type to store the coordinates of each point in a list of points. Here, we can make a contact abstract data type to store the data for each contact in our list of contacts.
Gamal: Yes! (Gamal opens their old project, but you don't need to.) Before, we used a point constructor to construct a list of coordinates.
Gamal points to the code for the point constructor:
point(X#)(Y#){report(list(X)(Y)}
Betsy: Then we used two selectors to select either the x or y coordinate.
Betsy points to the code for the coordinate selectors:
x coordinate of (point){report item (1) of (point)} and y coordinate of (point){ report item (2) of (point)}
Gamal: In this project, we'll need a contact constructor and selectors to access the name, address, and phone number for any given contact.
  1. Shortcut: You could paste the following text into the "Make a block" window to build the block more quickly.

    contact with name: %name address: %address phone: %phone

    The % signs make those words become input variables.
    Create a contact constructor that accepts three pieces of data as input: the contact's name, phone number, and address.
    contact with name: () address: () phone: ()
    It should report one whole contact (a list of the three items):
    contact with name: (Betsy) address: (123 Main St. #4, New York, NY 10001) phone: (212-555-1234) reporting {Betsy Anderson; 123 Main St. #4, New York, NY 10001; 212-555-1234}
  2. Write the selector blocks to retrieve the name from contact, address from contact, or phone from contact:
    contact name (contact block with inputs) reporting
    contact address (contact block with inputs) reporting
    contact phone (contact block with inputs) reporting
  3. Its somewhat artificial to use the constructor as the input to a selector; these images are just examples to show what the selectors should be able to do when given a contact as input. In your program, the selectors will take an item from the contact list as input and output the correct piece of that contact, like this:
    address from contact (item (1) of (contact list)) reporting '123 Main St. #4, New York, NY 10001'

    This will go somewhere else. Brian says this box is mostly about composition of functions, which is just a piece of functional programming... --MF, 6/29/18

    This project demonstrates a style of programming called functional programming. In functional programming, the output of one function is used as the input to another function rather than storing temporary data in a variable.

    There are many reasons to use functional programming:
    • It's easier to debug smaller, separate functions than one big one.
    • When you use variables to transfer data around your project, one part of your project might change a variable's value before the data gets where it's going. It's safer to put the output of one function directly into the input of another function.
    • If several parts of your program run concurrently (at the same time), functional programming avoids the chance of things happening in out of order because the outputs of functions that are used as inputs to other functions are always computed before they are used.
    • It makes your code easier to read.
  4. Declare input types for each selector to make it obvious that they expect a list (one whole contact) as input.
    name from contact: () address from contact: () phone from contact: ()
    You learned about Specifying an Input Type in Unit 2.
  5. Test your blocks together, and debug any problems.
    1. First, put the constructor (with input values) inside each selector (as shown above) to test that they each report the correct piece of data.
    2. Then, use the contact constructor to add a few contacts to your contact list.
      You can use these examples or make up your own:
      name address phone
      Betsy Anderson 123 Main St. #4, New York, NY 10001 212-555-1234
      Alphie Preston 149 E. 16th Ave., Sunnyvale, CA 94089 208-555-6789
      Gamal Abdel 369 Center St., Boston, MA 02130 617-555-1098
    3. Notice how your contacts appear in the list.
    4. Snap! has two different views for lists within lists. You can switch which view you see by right-clicking (or control-clicking) on the contact list watcher on the stage.
      If you don't see the watcher on the stage, make sure the checkbox beside the contact list variable in the Variables palette is checked. contact list watcher checked
      • Table View for Contact List Watcher:
        contact list watcher in table view
      • List View for Contact List Watcher:
        contact list watcher in list view
    5. Try selecting the name, address, or phone from a contact in your list
      name from contact: (item (random) of (contact list)) reporting Alphie Preston
    6. Now Is a Good Time to Save
    7. Debug any problems.