Snap! (formerly BYOB) is a visual, drag-and-drop programming language. It is an extended reimplementation of Scratch (a project of the Lifelong Kindergarten Group at the MIT Media Lab) that allows you to Build Your Own Blocks. It also features first class lists, first class procedures, and first class continuations. These added capabilities make it suitable for a serious introduction to computer science for high school or college students.
In the example below, a Snap! user can create new control structures, such as a
for loop, by writing a script as shown at the left. Once the
for block is created, it can be used even to make nested loops, as shown in the center. A sprite carries out that script at the right.
Snap! is presented by the University of California at Berkeley. It is developed by Jens Mönig at SAP, with design input and documentation by Brian Harvey at UC Berkeley, and contributions by students at UC Berkeley and elsewhere.
A data type is considered first class in a programming language if instances of that type can be
For example, numbers are first class in every language. Text strings are first class in many languages, but not in C, in which the relevant first class type is “pointer to a character.”
One of the limitations of Scratch as a language for computer science education is that its lists are not first class, so you can’t have a list of lists. Making lists first class is enough of an extension to Scratch to allow the creation of any other data structures (trees, heaps, hash tables, dictionaries, and so on) in user-defined Snap! code, implemented as lists of lists.
A less obvious limitation, because it is shared by many “adult” programming languages, is that procedures (blocks) are not first class. Making blocks first class in Snap! allows us to create control structures, such as the
for block above, as user-defined Snap! code. In particular, users can write the higher order list functions that are essential in the functional programming style.
Without being doctrinaire about it, we believe that in general, anything that’s in a language at all should be first class in that language. The Snap! object system introduces first class sprites, first class costumes, and first class sounds.
One example of a data type that exists behind the scenes in pretty much every programming language is the stack frame, the data structure that keeps track of active procedure calls and their local variables. Following Scheme, we make these data available to the programmer in the form of a first class data type called a continuation, which represents “the work still to be done after this point” as a callable procedure. Using continuations, the Snap! programmer can implement nonlocal control mechanisms such as catch/throw and threads.
Part of the genius of Scratch is the way it uses pictures to represent ideas such as loops, Booleans, and threads. The three different block shapes, for example, aren’t just a way to prevent syntax errors; they’re a way to teach the idea that some procedures return values and others don’t.
In Snap! we extend this visual teaching to ideas that have previously been considered too hard for young learners. For example, the screenshot on our home page includes this picture of a list of blocks. Experienced Scratch programmers will never have seen anything quite like this before, but they recognize the pictorial representation of a list in Scratch, and they know what blocks look like, and so it’s immediately obvious to them what they’re seeing. They realize without explicit teaching that blocks, which they’ve until then used only as program control elements, can also be used as data.