Suppose we want the squares of all the items of a list of numbers. That's a straightforward map
problem:
Now suppose instead that we want to select the odd numbers from a list of numbers. It's a little tricky figuring out how to tell if a number is odd, but apart from that it's a straightforward keep
problem:
But what if we want the squares of the odd numbers? This is neither a simple map
nor a simple keep
, but combines aspects of both. And we can solve the problem by using the value reported by the keep
as the list input to map
:
Don't be confused about which inputs do and don't have rings. It's the square function and the are-you-odd? function that we use as the first input to each higher order function. But, even though keep
itself is a function, it's the list reported by keep
that we're using as the second input to map
.
But there are two more steps, one at the beginning of the problem and one at the end, both related to the fact that people (the users of your program) want to see words and phrases in the form of text strings, but as you've seen, it's much easier to compute functions of those words and sentences if we have them in the form of lists, so we can use the higher order functions you're learning.
To translate from a text string to a list of words, Snap! provides the reportersentence->list
(pronounced "sentence to list") that takes a text string as input and reports a list of words:Once we have the list, we can use the tools we've already built to get a list of the first letters of the long words. What we want, though, is a single word as the reported value. We can use one of the higher order functions to join the letters into a string.
Try this: Should we usemap
, keep
, or combine
for this purpose?
Here's the final result:
Oops! The word "Joy" is shorter than five letters, but it's still an important word in the acronym. We need a better algorithm.
acronym
block so that, instead of keeping long words, it keeps words that start with a capital letter. (Hint: Experiment with the unicode of
block.)