Trying it out - Eliminating Chaos

Objective

After completing this lesson, you will be able to apply the concepts of the previous lesson - Eliminating Chaos.

Exercise

Your Turn!

Now it’s your turn. Download this document for a recap of the blocks we covered in this lesson and some hands-on exercises for you to explore.

What You Have Learned in This Lesson

In this lesson we have covered a few terms that are often mentioned in the context of computer programming. Here’s a short recap.

Algorithm

An algorithm is an unambiguous step by step description of a process to solve a certain problem. That process starts with an initial state and "input" and is then executed a finite number of times, eventually leading to an "output". This "output" is not necessarily the same all the time - some algorithms are deterministic, meaning for the same input they will always produce the same output. Some algorithms are non-deterministic and may result in different values even if the input is the same.

Algorithms are commonly described using natural language, pseudocode or flow charts.

In the case of the Chaos Game shown in the video, the algorithm in natural language could be:

  1. go to a random position inside the triangle
  2. pick a random vertex
  3. go half the distance from your current position to that vertex
  4. draw a dot there
  5. repeat the process starting at 2.

Emergence

Emergence describes a phenomenon, where a system has properties and behaviors that aren’t inherent to its basic building blocks and they come into existence through interaction between those basic building blocks. I.e. the whole is greater than the sum of its parts.

Emergence can occur in computer programs but also in real life. One example for a natural system with emerging patterns are snowflakes. The frozen water molecules form different shapes; these shapes depend on the interaction of the individual water molecules.

A computer program with emerging patterns is the Chaos Game shown in this lesson’s video.

Snowflakes are emergent patterns in nature, the Sierpinski triangle is the result of the Chaos Game algorithm.

Another word to remember is fractal – an infinitely self-similar shape. This is what you call shapes like the Sierpinski triangle. If you zoom in, there’s always yet another Sierpinski triangle in there.

Object-Oriented Programming (OOP)

In this lesson we also briefly touched on object-oriented programming, a programming paradigm, in which your code is structured around objects. Objects are "things with behavior", they react to messages send by other objects and process the obtained information based on their internal memory and "understanding". We call their vocabulary "methods" and their internal state "attributes".

One of the basic requirements of object-oriented programming is, that objects are what is called "first class" in a programing language. They can be used as an input of a function, can be the return value of a function, and can be assigned to a variable.

You can see all three requirements in the Chaos Game project. The expression item random of my other spritesreturns an object. That object is then assigned to a variable with the set variable _ to_ block. And finally, the object stored in the variable is used as an input to the distance to _ block.

The green sprite is the output of a function, is then assigned to a variable and finally used as an input to another block – the three requirements of first-class objects.

(The term "first class citizen of a programming language" seems very weird nowadays, it was introduced by Christopher Strachey in the 1960s and could probably be revisited.)