Combine
-ing DataPamela, should we teach/use the distinct
block? It's currently in the XML (fully built), but not discussed or used anywhere. --MF, 12/10/21
I don't feel a strong need to teach/use it. -PF
Next revision: decide whether to remove it. --MF, 12/27/21
Consider splitting this page into multiple pages. --MF, 1/11/23
In this activity, you'll create blocks to make Snap! do math with your data for you.
Snap! has another built-in block that is useful for data analysis: The block combines the elements of a list using an operation. For example, this expression adds all the values in the list 0, 1, 2.
Notice that the function used to combine the list items needs has two blank input slots. The keep
block only needs one blank in its input function, but combine
requires two slots since it combines an item with the item after it.
Unlike map
and keep
which also take functions as inputs, the combine
function is used with only a few input functions.
Combine
is mostly used only with these built-in functions:
Combine
takes a list of values as input. It combines the first two values using the input function, then combines that result with the third value using the function, then combines that result with the next value, and so on.
So, it can't matter to the input function in what order you group the items. Notice that (4 + 1) + 3 is the same as 4 + (1 + 3), but (4 − 1) − 3 is different from 4 − (1 − 3). With combine
, we don't use functions where order matters (such as subtraction or division).
+
, –
, ×
, and /
functions.
combine
to complete the block definitions for maximum of list
and minimum of list
blocks by dragging a maximum of list
or minimum of list
block to answer it.
sum of list
block so that it reports the sum of the items of a list.sum of list
block with a list of numbers before moving on.sum
block to answer it.
You can find the average of a set of numbers by dividing the sum of the values by the number of values.
average of list
block so that it calculates the average of the values in a list:Look through the palette on the left of the Snap! window. What blocks could help you do that in Snap!?
You'll need the length
and /
(division) blocks as well as the sum of list
block you just finished building.
average of list
block, and fix any problems.sum of list
block works first, then you wouldn't know whether any problems with your average of list
block were really problems with average of list
or were actually problems with sum of list
.average of list
block to answer it.
In this activity, you used the higher-order function combine
to create maximum of list
, minimum of list
, sum of list
, and average of list
blocks and used them to explore your data.