You can improve plural
to work correctly with more words. If the project is not already open, please reload it. (The suggested filename was U2-Plural
.)
list
of the words you want it to work for.plural
block should never give words to plural-h
unless those words end with "h". map
to test it on the entire list you made earlier.
plural
or plural-h
:Abstraction: Language often has special cases. In English, the plurals of some nouns add "s"; some add "es"; nouns like "calf" and "fly" become "calves" and "flies", changing their final letters before adding "es". And more. For a programming task this complex, it's (generally) best to break it into parts, handle each part separately with its own procedure (its own block), and then have the "top-level" block—in this case, plural
, itself—use those specialists. That is, instead of coding every little detail directly in plural
, it is cleaner and clearer to make plural
look something like this.
Showing the structure of the method—just the overall strategy—in the "top-level" block and leaving the details to separate blocks is one part of an important computer science idea called abstraction. Abstraction keeps your code clear, readable, and more easily debugged. It will also help your code be more flexible.
plural
to use the specialist. Test (you can use map
) to make sure plural
still works for all the words it used to work for, as well as the new ones.plural-y
to handle words like
plural
gives plural-y
only words it knows how to handle correctly.
Any better now? BTW, I moved what was TIF A up into an endnote in 1. --MF, 1/11/21
I cleaned this up a bit and added a "tough stuff" icon on part B. I think part B is very hard. More could be done to make it clearer, but I don't have the time for that now. One concern that this we should address now is not using "..." where what we need is jaggies. Can you please replace those two images with the complete images, and I'll jaggy them?
Also, can you make sure that the TG and the solutions match this version after you review it? Thanks! --MF, 1/11/21
plural
to correctly handle a input word that has a space at the end.
Right now, if plural
is given a word with a space at the end, it leaves that space in the plural:
Figure out how to handle this special case and edit plural
so that the result is
You already have a block that specializes in making plurals of words that don't have a space at the end. Use it.
Surprise! Once plural
works for a single space at the end of a word, try giving it . That works too! But why?
plurals
block still feel cluttered, even though the details about how to handle each possible last letter are abstracted into specialist blocks? One way to improve on this situation is to use another kind of abstraction called data-directed programming.
list
, then right-click on the block and choose "ringify." As the "x" example shows, it doesn't have to be a block named plural-
something. It just has to have an empty input slot where you want the word to go.plural
procedure, you can replace most of the if
blocks withFind first
is a higher-order function works similarly to keep
, but it reports only the first item that's found. It is equivalent to item (1) of (keep)
.
The call
block in the palette doesn't include the "with inputs
" in the picture; when you click on its right arrowhead, those words will appear along with an input slot. The call
block's first input slot is the empty gray ring, which means that call
wants a block in that input slot. When you drag item 2 of pair
into that slot, the ring remains around it. That's usually what we want when using call
, but not this time. Right-click on the item of
block and choose "unringify" from the menu.
call
to see what else you can do with it. What happens if you don't unringify the item
block? What happens if you leave out the "with inputs
" and the second input?plural
so that it tests all trailing substrings of the word against the specialist list. That is, if the word is "ditch," you'd look in the specialist list for "ditch," then for "itch," then "tch," "ch," and finally, if nothing else is found, "h." You'd putword
function that just reports its input; it's needed because you can't type letters directly into a ring.) Now you have a really uncluttered plural
block. And it'll even work for a language other than English, if you make a different specialist list.
if
any more. Your plural
block can now be just one-line: report (call (item 2 of (longest trailing substring ...
.