plural
block you made in Unit 2.
Here's the plurals
block from Lab 1. It takes a list of words as input, and reports a list of the plurals of those words. Although there is a way to build this block using map
, bear with us: there is a good reason to investigate this recursive version.
The big idea here is that you can divide a list into its first item and all the rest of the items, form the plural of the first one, and make a recursive call for the rest. The in front of
block may look strange at first, with its asymmetrical inputs (one item, one list), but it puts the plural of the first word and the plurals of the rest of the words back together.
This pattern of code can be used to build many useful recursive reporters that have a simple base case and a simple recursive call.
Do the following exercises using recursion, not higher-order functions.
plurals
.squares
block that takes a list of numbers as input, and reports a list of the squares of the numbers.
exaggerate
block that takes a sentence as input, and reports a sentence in which every number in the input is doubled, "good" is replaced by "great," "bad" is replaced by "terrible," "like" is replaced by "love," and "dislike" is replaced by "hate."
To do this, use a "wrapper" function that transforms the sentence into a list of words and back again:
You'll also want a helper function exaggerate word
that works on a single word, looking for specific words and types to exaggerate.