Data Types

primitive types

A data type is, just as the name suggests, what kind of data something is.

Each programming language provides some primitive (built in) data types. The primitive types in Snap! are number, text string, Boolean, list, command, reporter, and predicate (but we haven't used the last three as data types yet). You can get a reminder of Snap!'s primitive types from the pull-down menu of the is () a ()? block (shown right).

Abstract data types (or ADTs) such as "noun phrase" are custom data types that exist only in the mind of the programmer. For example:

 
Gamal joins Betsy and Alphie's discussion about using a list of coordinates to make a drawing of a letter.
sprite drawing letter A by going to each set of coordinates
Gamal: I think you had trouble finishing that last script because those item of blocks get confusing with lists inside lists.
Betsy: Yeah, the expressions like item (2) of (item (1) of (A)) get confusing fast!
Gamal: Well, we could define a new data type called point () () that contains an x-coordinate and a y-coordinate.
Gamal's idea is called data abstraction: creating a new abstract data type so that the detailed representation of the type is hidden from its users. In this case, users of points don't have to know that they're represented as lists.
Gamal has built this block: point = list
Betsy: That block is trivial; it isn't going to make the rest of the program any shorter. Point does the same thing as list!
Gamal: But it is going to make it easier to think about. Now, a list of points will actually look like a list of points:
letter A as list of points
Gamal's point () () block is called the constructor for the point abstract data type because it constructs (and reports) the list of assigned inputs (in this case, the x and y coordinates for a point).
Betsy: Hmmm... So we could also make X coordinate and Y coordinate blocks so that the drawing script would have things like X coordinate of item (1) of (A) instead of item (1) of (item (1) of (A)), which was confusing too.
Betsy's two other blocks: x coordinate = item 1 y coordinate = item 2
Betsy's two blocks x coordinate of {} and y coordinate of {} are called the selectors for the point abstract data type because they each select one component of a point.
  1. Improve your "U3L2-DrawShape" project from the previous page using Betsy's and Gamal's ideas.
  2. Save Your Work

    Make a general draw shape () block that draws any shape based on a list of coordinates using the constructor and selectors designed by Gamal and Betsy, respectively.

  3. Notice that the input slot rectangle with two smaller orange rectangles inside shows that a list value is expected. This animation shows you how to specify the list input type:
    script with for each (item) of (A) {glide 1 secs to x:(blank) y:(blank)} then glide to (0, 0)
 
  1. Make a new list of lists with coordinates to draw a different letter or shape. 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 {A, B, C} hides all the complexity of the lists of coordinates used to draw the shapes.
    animation that shows drawing H then drawing I