Snap! Primitives in Other Sequential Languages

On this page, you will learn about similarities between Snap! and other common sequential languages.

Central programming language features having to do with flow of control (Control), data management (Variables and Lists), and computation (Operators and a few Sensing blocks) have direct equivalents in most languages.

  1. Look over the following table of examples to see how some of the blocks you've learned in Snap! are coded in two other sequential languages.
                Snap!            PythonJavaScript
    <code>for</code> block
    for i in range (1, 11):
                action()
    for (i=1; i <= 10; i++) {
                action()
            }
    <code>repeat until</code> block
    while (not condition):
                action()
    while (not condition) {
                action()
            }
    <code>if/else</code> block
    if condition:
                action1()
            else:
                action2()
    if (condition) {
                action1()
            } else {
                action2()
            }
    <code>if/else</code> reporter block
    yes if condition else no
    (condition ? yes : no)
    <code>report</code> block
    return value
    return value
    <code>stop this block</code> block
    return
    return
    <code>function ()</code> block in a gray ring
    lambda x : some_function(x)
    function (x) {
                return someFunction(x)
            }
      or
    (x) => someFunction(x)
    <code>a times b</code> block
    a * b
    a * b
    <code>a to the power b</code> block
    a ** b
    a ** b
    <code>a and b</code> block
    a and b
    a && b
    <code>join (hello) (world)</code> block
    " ".join(["hello","world"])
      or
    "hello " + "world"
    "hello".concat(" ","world")
      or
    "hello " + "world"
    <code>set (foo) to (87)</code> block
    foo = 87
    foo = 87
    <code>change (foo) by (5)</code> block
    foo += 5
    foo += 5
    <code>list (John) (Paul) (George) (Ringo)</code> block
    ["John", "Paul", "George", "Ringo"]
    ["John", "Paul", "George", "Ringo"]
    <code>item (1) of (value)</code> block
    value[0]
    value[0]
    <code>all but first of (value)</code> block
    value[1:]
    value.slice(1)
    <code>map (some function) over (value)</code> block
    list(map(some_function, value))
      or
    [some_function(x) for x in value]
    value.map(someFunction)
    <code>for each (item) of (value): action(item)</code> block
    for item in value:
                action(item)
    value.forEach(action)

This is just a small sample of the number of primitive commands and functions in any programming language. Luckily, you don't have to memorize them; you can do a web search for "javascript list item," for example, to learn any particular one as you need it.

I haven't done a super close read, but I'm anticipating hinting most of the rest of this page. Except maybe Libraries... --MF, 12/20/21

By the way, look at the Python join in the table. It has the very nice feature of allowing you to choose a separator to go between the joined strings:

>>> " ".join(["John", "Paul", "George", "Ringo"])
'John Paul George Ringo'
>>> ",".join(["John", "Paul", "George", "Ringo"])
'John,Paul,George,Ringo'

In the first interaction, the string " " provides the separator; in the second, the string "," does. That's an elegant way to extend the idea of joining strings, avoiding the need to put spaces in the input strings. (You could also use an empty string to get the exact effect of the join block in Snap!.)

On the other hand, look at the Python version of map. Why do you have to wrap a list(...) around the call to map? It's because Guido van Rossum, the designer of Python, hates higher order functions, so when his users insisted on having them, he made it as painful as possible to use them. Python's map doesn't return a list; it returns this strange other thing that you have to convert to a list. (He wants you to use the for x in notation instead.) Similarly, he hates anonymous procedures (Snap! gray rings), and so when his users insisted, he grudgingly allowed anonymous reporters that fit on one line, but not longer reporters or any anonymous command scripts. Python could be a really usable language if he didn't have his head wedged on these issues.

But look at the Python equivalent of for. Why does range(1,11) mean "the numbers from 1 to 10"? The idea is that you're using those numbers to select items from a list, but instead of numbering the items, Python numbers the spaces between items, just as when you're using a word processor it displays the cursor as a vertical line between two letters. So range(1,11) means "the items to the right of gap number 1 and to the left of gap number 11." You can learn to think that way, but it's not obvious to a beginner. (It's a little more intuitive if you start at position 0, before the beginning of the list; range(0,5) selects five items from a list, and you can abbreviate it as range(5).)

What do you think about using + to join strings? It's meant as an analogy with arithmetic; you are "adding" one string to another. Using the same symbol to mean two different things in different contexts is called "overloading." It's not too bad in Python, which is very strict about the distinction between a number and a string of digits. (2+"3" gives an error message.) But JavaScript, like Snap!, will automatically convert a number into a string of digits if necessary, so 2+"3" is "23" even though 2+3 is 5. (The automatic conversion isn't a problem in Snap! because Snap! doesn't overload the + operator.)

The other block categories, the ones having to do with graphics or sound, don't really have analogs in other languages. Instead, just as Snap! has libraries, other languages also have libraries to provide such capabilities. For example, if you want JavaScript equivalents to the turtle graphics blocks (move (n) steps, etc.), you start in your search engine; "JavaScript turtle graphics" will get you to https://github.com/bjpop/js-turtle from which you can download the file "turtle.js" to include with your own JavaScript program. (On the other hand, because the main purpose of JavaScript is writing programs that will be run inside a browser window, it does have built-in support for the sort of graphics you see in typical browser windows: scrolling text boxes, form-filling, checkboxes, etc.)

Libraries are a little easier in Python because there's a central repository of them. So you can draw a square this way:

from turtle import *

pendown()
for i in range (4):
    forward(100)
    right(90)

How Can You Learn These Similar Languages?

I didn't do any of this. W3schools is a little controversial. Some people swear by them; others say they're full of inaccuracies. I don't know enough to have an opinion. But, anyway, kids know how to learn stuff; you search the Web, or you get a book from the library. Or you take APCSA next year.

The kids who could learn programing without us "know how to learn stuff." I'm not convinced that it has to be W3, but I think it would be lovely to (and perhaps even a shame not to) end this page with some "next steps" for kids to explore on their own time. --MF, 12/20/21