Trying it out - How to Work with Data

Objective

After completing this lesson, you will be able to apply the concepts of the previous lesson - How to Work with Data: A Short Intro to Programming Paradigms.

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 you have learned about different programming paradigms.

A programming paradigm is a high-level approach to designing and coding a program. It describes the overall style of structuring and implementing a solution, including which programming language or features of a programming language are used to build it.

Imperative programming

In the imperative programming paradigm, you work with explicit step by step instructions (commands) to describe how a program should operate.

You use control structures like loops and conditionals to manage the execution flow of your program. Imperative programs often have state changes, meaning that e.g. variable values are updated (mutated) or attributes of an object change while a program is running.

The example for an imperative program in this lesson was Alonzo visiting his friends.

The actions are performed sequentially using the for each loop. You can see the result of each step of the computation happening on the stage as Alonzo is changing his position.

The script on the left is executed by Alonzo. It’s using a loop to iterate over the list of Alonzo’s friends (“my other sprites”). The steps are performed sequentially and Alonzo visits one friend after the other, starting with the crab, then going to the dog and ending at the dinosaur while changing Alonzo’s state each iteration.

Functional programming

The functional programming paradigms treats functions as first-class (also check Unit 2 Lesson 1 and 2) – they can be used as an input or output to another function and can be assigned to variables.

You construct programs by nesting functions and applying them to values. The outcome of a program written with the functional programming paradigm is the result of a series of function evaluations performed on the start value.

Functional programs avoid state and mutable data.

In this lesson you saw the example of map and keep– two higher order functions (HOFs) often used in functional programming. A higher order function is a block that takes a function as an input or reports a function as an output.

Using the map block with the distance to _ function on the list of Alonzo’s friends (my other sprites) resulted in a new list containing the distance value from Alonzo to each other sprite.

The list on the left is the result of the “my other sprites” block. Each item in that list is filled into the empty input slot of the “distance to _” function in the grey ring of the “map” block. The result of that computation is reported to the new list that is the result of the whole “map” expression. The first item in the result list therefore is the distance from Alonzo to the dinosaur.

Keeping the friends whose distance to Alonzo is smaller than 150 steps filtered the list of all friends and reported a new list containing just the ones for which the condition returns true.

The script on the left is executed by Alonzo. It’s using a loop to iterate over the list of Alonzo’s friends (“my other sprites”). The steps are performed sequentially and Alonzo visits one friend after the other, starting with the crab, then going to the dog and ending at the dinosaur while changing Alonzo’s state each iteration.

Hyperdimensional programming

Hyperdimensional programming is an approach often used in data science and for artificial intelligence. Information is represented as vectors (lists in Snap!) and functions can directly operate on those vectors; processing entire sets of values in one step.

In the video hyperdimensional programming was visualized through the custom average block. If you put in a list of numbers, the average block will report a single number, the arithmetic mean of the numbers in the input list. If you input a table (e.g. like the position of my other sprites), it reports a list, each item in that list being the arithmetic mean of the corresponding column of the original table.

The custom “average” block reports the arithmetic mean of its input. If the input is a list of numbers, the result is a number, if the input is a list of lists (like the table that’s reported by the “position of my other sprites” expression), the result is a list. By the way, directly getting the positions of all other sprites in one step is also hyperdimensional programming.