ST EK List:
5.5.1I Lists and other collections can be treated as abstract data types (ADTs) in developing programs.
Abstract Data Types
Ruthless Suggestions from Al
- Comment: This is the kind of thing I wish we could do more often: Present a method as in 2.2.2 and then show the power of a more abstract method (2.2.3). If this becomes a regular feature, I think it will save time overall. Sorry: I meant the transition from 2.2.1 to 2.2.2. That's a very good design principle.
- I'm a little confused by what you mean here. 2.2.2 seems like a more abstract version of 2.2.1, but I'm interested in the principle you are explicating. Can you say more? Do we do this anywhere else--as another example? --MF, 5/16/18
On this page, you will create an abstract data type
point
to make your U2L2-DrawShape code easier to read, debug, and use:
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.
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:
to construct the ADT
-
and
to select either coordinate of a point
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: I was looking at our
.png)
block and thought of a way to make it much easier to read.
Betsy: Yeah, expressions like

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

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:

and
Betsy: Hmmm... And if we make a
().png)
block, then we can write

instead of

. That would make it clearer what this list really is.
Betsy builds this block. She marks the
x and
y input variables as numbers.
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 you create when you select the
number input type:

The input type also changes the appearance of the input slot to indicate what kind of input is expected.
The
().png)
block is called the
constructor of this new abstract data type. The

and

blocks are called the
selectors for the
point
ADT because they each
select one component of a point.
- If your U2L2-DrawShape project is not already open, open it now.
- Improve your
go to point
block (from "U2L2-DrawShape") by using selectors inside the go to point
block as Betsy and Gamal described.
-
Convert that script into a general
block that takes as input a list of points and draws that shape.
- 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.
-
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

hides all the complexity of the lists of coordinates used to draw the shapes.
-
Change your project so that
draw message
draws the letters next to each other instead of replacing one with another: