Page 1: Creating the Contact ADT
Unit 3, Lab 2, Page 1
On this page, you will develop an Abstract Data Type to store and access contact data (name, address, phone number, etc.).
A note about privacy: . While Snap ! projects are not public, any data you enter into them is saved as part of the project. So in the interest of preserving everyone’s privacy you should not put your or your classmates’ actual personal information into your contact list.
Setting Up the Contact List
Create a global
variable.Why global? Because multiple scripts across the project will use this variable, it needs to be accessible by all of them. So it can’t be a script variable or input in any one script.
Initialize
as an empty list.
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
.
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 quiz item abstract data type to store the questions and answers in a list of quiz items. And we used the same pattern in the flashcards and survey projects. Here, we can make a contact abstract data type to store the data for each contact in our list of contacts.
Gamal: Yeah. In this project, we’ll need a contact constructor and then selectors to access the name , address , and phone number for any given contact.
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: %phoneThe percent (%) signs make those words become input variables.
Create a
contactconstructor that accepts three pieces of data as input: the contact’s name , phone number , and address .
It should report one whole contact (a list of the three items):

Write the selector blocks to retrieve the
name from contact,address from contact, orphone from contact.


It’s 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:

You learned about input types and output types (domain and range) on Unit 2 Lab 3 Page 1: What’s a Predicate? .
It’s important to make sure that the actual inputs to a block match the input type the block expects. At the most basic level that Snap! knows about this means that if a block expects a list that the actual input is a list.
But that’s not always precise enough. For example, a selector like
address from contactexpects its input not only to be a list but list in the specific shape that represents acontactas produced by thecontactADTs constructor.If you call
address from contactwith an input that isn’t a list you’ll get an error from Snap! But if you call it with an input that is a list but isn’t a list representing acontact, such as the list of all the contacts stored in the
variable, it’s not going to work properly, in the sense that it won’t return a string representing an address.Learning to to think about what kinds of values blocks expect and what kinds of values they report so you can connect blocks together properly is a key programming skill.
Declare input types for each selector to make it obvious that they expect a list (one whole contact) as input.

You learned about Specifying an Input Type on Unit 2 Lab 2 Page 2: Planning a Quiz App.
Test your blocks together, and debug any problems.
First, put the constructor (with input values) inside each selector (as shown above) to test that they each report the correct piece of data.
Then, use the
contactconstructor to with
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 408-555-6789 Gamal Abdel 369 Center St., Boston, MA 02130 617-555-1098 Try selecting the
name,address, orphonefrom a contact in your list
Debug any problems.