In this lab you will sort your contact list alphabetically by name.
On this page you will review the technique of recursion.
nested triangle
block.
Your solution may not be exactly this, but it's probably close:
A top-level script is one that's in the scripting area, not inside the definition of a block.
An invocation is a use of a block. For example, makes two invocations of
+
. The verb form is invoke.
nested triangle
from a top-level script, how many times does that invocation (not counting invocations within invocations) run the purple block that invokes nested triangle
?nested triangle
draws the three lines shown in black in the picture below?if
block in this code? (If you're not sure, you might try editing nested triangle
to remove it, and see what happens.)A recursive problem (the thing you're trying to do, not the code to do it) is one in which the desired result includes a smaller version of itself, such as the green (or red or blue) fractal inside the bigger fractal. A recursive procedure (the actual code) is one that calls itself inside its definition. Do you see why a recursive procedure is likely to be a good solution to a recursive problem?
If a recursive procedure calls itself, and that call also calls itself, and that call also calls itself... the result will be an infinite loop, which means that the program will keep running forever (or until it runs out of memory). To avoid that bug, every recursive procedure must have a base case, a simple input for which the procedure can do its job without calling itself. (The base case of nested triangle
happens when size ≤ 2; in that case the procedure doesn't have to do anything.)
The general pattern for a recursive command is
Compare this with the nested triangle
block above. (In that one, the base case is particularly simple: it does nothing at all.) Most recursive commands follow this pattern, except that in certain cases there's more than one recursive call.
It's therefore tempting to try to write a recursive reporter this way:
But in fact you can't stack report
blocks; once your block reports a value, it's finished. (This is why there's no connection tab at the bottom of the report
block.) Instead, you have to use a combiner function to put the pieces together into a single thing to report:
The combiner can be any function with the correct range (the kind of value it can return). So if your custom reporter is supposed to return a sentence, the combiner might be join
or join words
; if it's supposed to return a number, you might use +
or ×
.
sum of
block with no additional help, do it now. Otherwise, fill in the three blanks in the definition this way:
if
block.all but first of
the list? You really don't need this hint.
map
and keep
, combine
requires a function with two empty input slots.map
and keep
, combine
is mostly used with one of only these six functions in its first input slot:sum of
using combine
.map
.
unicode of
block will be helpful here.map
to generate a new list containing only the first letters of the words in your input list, and then keep
only those that are capital letters.keep
only the words that begin with capitals and then use map
to create a list containing only the first letters of those words.