Warning: the material in this lab is quite challenging. It is highly recommended that you work with a partner.
So far, all our examples of recursion have been command blocks, whose scripts have a structure like this:
There can be any number of recursive calls interspersed with other commands in the script.
Reporters are slightly trickier, because a reporter can only report one value. So you can't do this:
(In fact, Snap! won't let you stack report
blocks; since report
ends the script, there is no connection tab below the block and you can't put anything directly below it. Optional exercise for hotshots: How did we make that picture?)
Instead, most of the time a recursive reporter call will be inside a combiner that attaches the result of the recursive call to some other value:
The combiner can be an arithmetic operator such as +
or ×
:
or a text string operator such as join
or join words
:
or a list operator such as append
or in front of
:
Note: Occasionally, the recursive call does appear directly inside the report
:
(Wikipedia article on pig latin. Our block only accounts for the first rule.)
But most of the time, you won't go wrong if you start by thinking about what combining block to use.
☞ Don't go on until you understand what those four example scripts do, and how they do it.