Abstract Data Types

Change filename.

Ruthless Suggestions from Al

Teacher feedback from 2017-2018: despite the strong intention of understanding abstract data types and making a point block, this was very difficult for students to understand, as they just could not relate, lost interest and didn’t really understand the why behind what they were doing --MF, 3/6/19

PG: clean up

On this page, you will create an abstract data type point to make your U2L2-DrawShape code easier to read, debug, and use:
draw shape(list{point(-50)(20), point(-10)(120),...point(-30)(20)})

moved to new first page

Just as the name suggests, a data type is what type of data something is (number, text string, list, etc.). Each programming language provides some primitive (built in) data types. Snap! provides numbers, text (words and sentences), lists, Booleans, and some you haven't yet used as data:

This block's pull-down menu shows all of Snap!'s primitive types.
UPDATE IMAGE --MF, 10/18/18
primitive types: number, text, Boolean, list, sprite, command, reporter, predicate

An abstract data type (or ADT) is a kind of data that's meaningful to your program but not built into the language. You develop it as you program.

For example in this lab, you'll create a point data type with:

The word "abstract" is often used casually to mean something harder to understand or more complex, but in computer science, its meaning is almost the opposite. ADTs are things that you, the programmer, create to make your program easier for you and others to read, debug, and improve.

 
Gamal joins Betsy and Alphie's ongoing discussion from previous page about using a list of coordinates to draw a letter.
Gamal: I was looking at our go-to-point() block and thought of a way to make it much easier to read.
Betsy: Yeah, expressions like item(2)of-point are hard to read. Even after I wrote it to find the y coordinate, I had to think twice to understand it.
Gamal is suggesting data abstraction: creating a new ADT to hide the details. When using the point ADT, you won't have to think about how points are represented as lists.
Gamal: That's what I mean. Let's make it clearer. We can define a y coordinate of (point) block to use instead of item 2 of .
Betsy: But that block would do just the same thing as item 2 of, so what's the use? It won't make the program shorter.
Gamal: Yes, but it'll make the program easier to read and easier to think about. We can use x coordinate and y coordinate instead of item of as inputs to the go to block.
They build two blocks: x coordinate of (point){report item 1 of (point)} and y coordinate of (point){report item 2 of (point)}
Betsy: Hmmm... And if we make a point () () block, then we can write list{point(-50)(20), point(-10)(120),...point(-30)(20)} instead of list{list(-50)(20), list(-10)(120),...list(-30)(20)}. That would make it clearer what this list really is.
Betsy builds this block. She marks the x and y input variables as numbers.
point(X#)(Y#){report(list(X)(Y)}
Don't type the # symbol in the point block's input names. Just like the symbol for inputs that you declared to be lists, the # is not part of the input's name but is a type hint that Snap! shows when you select the number input type:
selecting the Number input type
The input type also changes the appearance of the input slot to indicate what kind of input is expected.

moved to new first page

The point () () block is called the constructor of this new abstract data type. The x coordinate of () and y coordinate of () blocks are called the selectors for the point ADT because they each select one component of a point.
  1. If your U2L2-DrawShape project is not already open, open it now.
  2. Improve your go to point block (from "U2L2-DrawShape") by using selectors inside the go to point block as Betsy and Gamal described.
  3. Save Your Work Convert that script into a general drawShape() block that takes as input a list of points and draws that shape.
 
  1. Make a new list of points to draw a different letter or shape. This time, use your point constructor in the list of points. Check to see that your draw shape () block works with this new list.
  2. If you made other letters, write a draw message () block that takes a list of letters as input and uses draw shape () several times to draw a message.
    Notice the use of abstraction. Representing the message "HI" as list (H)(I) hides all the complexity of the lists of coordinates used to draw the shapes.
    animation that shows drawing H then drawing I
Save your work
  1. Change your project so that draw message draws the letters next to each other instead of replacing one with another:
    HI