About Snap!

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[1] lists, first class procedures, first class sprites with inheritance[2], and first class continuations[3]. 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 (which isn’t built into the language), 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! runs in your browser. It is implemented using Javascript, which is designed to limit the ability of browser-based software to affect your computer, so it’s safe to run even other people’s projects, even if you don’t trust our competence or good intentions.

Snap! is presented by the University of California at Berkeley. It was developed by Jens Mönig at MioSoft Corporation (now at SAP), with design input and documentation by Brian Harvey at Berkeley, and contributions by students at Berkeley and elsewhere.

One of the motivations for developing Snap! is that it's used in the Berkeley course Beauty and Joy of Computing (BJC), taught on campus as the computing breadth course for non-CS majors, and made available in collaboration with Education Development Center, Inc., as a high school course that prepares students for the Advanced Placement Computer Science Principles exam.

What  follows is a slightly more technical explanation of what makes Snap! special.

First Class Data Types

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 limitation of Scratch that's less obvious, 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. (The parameter action in the for block's definition represents the script inside the C-shaped slot in the block. The instruction run action invokes that script.) 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.

Object Oriented Programming

The Snap! object system provides first class sprites, first class costumes, and first class sounds.

Scratch sprites are objects: They have local methods and local variables. They lack only two things: They're not first class, so, for example, you can't make a list of sprites; and there is no inheritance mechanism, so you can't fix a bug in one sprite's method and have the fix appear in all existing children ("clones") of that sprite. Snap! adds those capabilities. Sprite attributes other than methods and variables, such as position and direction, are also inheritable.

The typical class/instance object system lends itself to large programming projects that are fully designed before being implemented. But a class is abstract and invisible, so that approach doesn't lend itself to a visual, interactive language. Snap! instead uses prototyping, so that a user can interactively program a sprite and then use it as the template (the parent) for other sprites. When a user examines a child sprite, Snap! gives visible feedback about inherited versus locally shadowed properties:

This sprite is inheriting its x position and direction, but has its own independent y position.

Continuations

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.

Visual Representation for Advanced Ideas

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.

This material is based partly upon work supported by the National Science Foundation under Grant No. 1138596. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.