Recursive Reporters

Until this unit, all our examples of recursion have been command blocks, whose scripts have a structure like this:

recursive command pattern

There can be any number of recursive calls interspersed with other commands in the script.

Reporters are different, because a reporter can only report one value. So you can't do this:

bad (multi-report) recursive reporter pattern

Take It Further:
How did we make that picture?

(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.)     

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:

correct recursive reporter pattern with combiner

The combiner can be an arithmetic operator such as + or ×:

factorial

Do the problems on this page using recursion, not higher order functions.

  1. Make a sum block that takes a list of numbers as input and reports their sum.

Note: This problem and the others on this page are really too simple to need recursion; in real life you'd solve them with looping or with higher order functions. Right now we're trying to get you comfortable with the notation of recursive reporters. Starting with the next lab, you'll be working on problems that really are natural for recursion.

... or a text string operator such as join or join words:

list->sentence

  1. Make an initials block that takes a list of words as input and reports a word containing the first letters of each of the input words.

or a list operator such as append or in front of:

plurals

Don't go on until you understand what those three example scripts do, and how they do it.

Note: On rare occasions, the recursive call does appear directly inside the report:
piglatin
But almost always, you won't go wrong if you start by thinking about what combining block to use.