Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active February 18, 2025 19:46
Show Gist options
  • Save CliffordAnderson/391b18399855585dc3f7b34782533998 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/391b18399855585dc3f7b34782533998 to your computer and use it in GitHub Desktop.
Yale Library Coding Meetup

Introduction to Block-Based Programming

Learning Goals

  • Develop facility in block-based programming by creating custom blocks and drawing on the stage.
  • Explore the concept of recursion by encoding the Fibonacci sequence.
  • Understand how block-based programming fosters learning by visualizing units of computation.

A Little Theory

  • Constructivism is a pedagogical theory inspired by Jean Piaget that understands learning as an active process of experimentation, reflection, and social interaction.
  • Constructionism is a pedagogical approach that emphasizes learning by experimenting with artifacts and reflecting on those experiments. Seymour Papert, an MIT professor, co-developed the Logo programming language to provide children with a way to learn mathematics through by playful experimentation.

Where to Find Snap! and NetsBlox

  • Scratch, a block-based programming language for K-8 computer science education created at MIT by the Lifelong Kindergarten research group.
  • Snap!, a block-based programming language developed at the University of California and SAP for teaching computer science at a high school and college level.
  • NetsBlox, a version of Snap! that integrates peer-to-peer networking and remote procedure calls.

Learning Your Way Around

We will start by learning our way around Snap!/NetsBlox. Our goal is to draw a simple line like the one below. We'll pull over blocks from the block menu, move them onto the scripting area, connect them together, and then click on them to see what the sprite draws on the stage. Ready to try this out? Let's go!

image

Fibonacci Sequence

In our coding experiment today, we will create a function to generate the famed Fibonacci sequence. The Fibonacci sequence is defined as follows:

$$ F_0 = 0, \quad F_1 = 1, \quad F_n = F_{n-1} + F_{n-2} \quad \text{for} \quad n \geq 2. $$

Here are the first 10 numbers in the Fibonacci sequence:

Ordinal Position (n) Fibonacci Number (F_n)
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55

We will now translate this mathematical formula directly into blocks. We are going to create our own Fibonacci (or "Fib") block that takes an integer (a whole number) and reports the number at that location in the Fibonacci sequence. We'll use a control structure called an if/else to create a simple decision tree.

fibonacci circle script pic

What do you notice about how this block computes numbers in the Fibonacci sequence? What kinds of inputs might cause the program to fail?

Fibonacci Braids

We will now draw on the stage with a sprite, creating a colorful braid with our Fibonacci block. Here is the set of blocks that we will use.

fibonacci circle script pic (1)

After you snap these blocks together, your sprite should draw a braided circle on the stage that constantly changes color.

Stage__3_-removebg-preview

If you have any difficult putting the blocks together, here is a complete version of the program that you can open and customize.

Fibonacci Spiral

In this section, we'll inspect a program that I wrote to produce the Fibonacci tiles and spiral. This program uses the same Fib block that we created for the braid but additional custom blocks to create tiles and a spiral. Can you put together these blocks to create an image like the one below?

Stage (4)

Here is a program with the blocks that you will need. See if you can figure how best to stack them together.

fibonacci spiral script pic

As you experimented with snapping these blocks together, did you discover any happy accidents? If so, please share them in the comments below!

Next Steps

If you are interested in learning more about block-based programming, you are welcome to take a MOOC on Coursera that a few colleagues and I put together called Programming for a Networked World.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment