Students have touched on lists in Unit 1, but this lab is where we really dive into them. First, in the very simple shopping list app, students learn list creation and mutation. Then, in the quiz program, we introduce the very important idea of data abstraction, specifying a constructor and two selectors for the quiz item
data type.
class QuizItem { field question; field answer; }(That's not any particular language, just a stripped-down syntax to make the point.) But in Snap! we use procedures instead: the constructor
But what we don't do in this lab is to introduce the higher-order functions that will be our main tools for list processing later in the course. We hint at them in the third page by using for each item
, which is a higher-order procedure but not a function, and therefore doesn't require the gray ring notation that will be introduced in the next lab.
when I am clicked
block allows a sprite to be used as a button.The table below compares the "ordinary language" use of abstract with the CS use. (It's not about data abstraction specifically, but the major levels of abstraction in computer science generally.)
In ordinary language, the actual physical computer is the "least abstract" thing, because you can hold it in your hand, push its buttons, maybe take it apart. In ordinary language, programming seems to deal with "more abstract" ideas, things with no visible, tangible existence. In some ways, the physical thing might seem easiest to think about, and the programming at the top of the table harder.
Computer scientists use "abstract" to mean something more like "this is defined in terms of that": application software is written in a high level language, which is itself implemented in a low level language. It's easier to understand a language than to deal with all the details—how the language is built from lower level languages, and how they work with the computer architecture, and how that's built of transistors, which depend on the behavior of electrons, and so on. From that perspective, things get harder as you move down the table. It's easy to write a program in Snap!, harder to write one in C, because you have to think about more details of memory management. It's easier to understand the design of a computer architecture (the Intel Core design, for example) than to understand the physical circuits that implement it.
Ordinary language | Levels of abstraction | Computer science |
---|---|---|
most abstract (harder) | application software | most abstract (easier) |
high level language (Snap!) | ||
low level language (C) | ||
machine language | ||
computer architecture | ||
least abstract (easier) | physical computer (chips, wires) | |
transistors | ||
atoms | ||
protons, neutrons, electrons | ||
most abstract (harder) | quarks | least abstract (harder) |
for each item
block, which hides the use of index numbers to access each item. The programmer can just write the code to handle one item, and the block takes care of supplying items one at a time.for each item
block and try to do the traversal in the script in the C-shaped slot.item
variable provided by the block contains an index (an item number), whereas it actually contains the item itself.[value1, value2, value3…]to create a list with those values as the first, second, third, and so on items. For example,
aList ← [value1, value2, value3,...]creates a new list that contains the values
value1,
value2,
value3, and
…at indices 1, 2, 3, and … respectively and assigns it to
aList.
aList ← []creates a new empty list and assigns it to
aList.
aList ← bListassigns a copy of the list
bListto the list
aList. For example, if
bListcontains
[20, 40, 60], then
aListwill also contain
[20, 40, 60]after the assignment.
x ← aList [i]assigns the value of
aList[i]to the variable
x.
aList[i] ← xassigns the value of
xto
aList[i].
aList[i] ← aList[j]assigns the value of
aList[j]to
aList[i].
INSERT(aList, i, value)shifts to the right any values in
aListat indices greater than or equal to
i. The length of the list is increased by 1, and
valueis placed at index
iin
aList.
APPEND(aList, value)increases the length of
aListby 1, and
valueis placed at the end of
aList.
REMOVE(aList, i)removes the item at index
iin
aListand shifts to the left any values at indices greater than
i. The length of
aListis decreased by 1.
FOR EACH item IN aList { <block of statements> }The variable
itemis assigned the value of each element of
aListsequentially, in order, from the first element to the last element. The code in
block of statementsis executed once for each assignment of
item