Modeling Language: Plurals, Part 2
Unit 2 Optional Project
Need to redo KEEP images. –MF, 6/27/20
- REVISION NOTE: Students EXTEND their list of nouns as needed. At each testing stage, have them map their “specialist block” or their plural block over the entire list and note which words are and aren’t correctly pluralized.
- Rebuild the images without white backgrounds. –MF
You can improve plural
to work correctly with more words. If the project is not already open, please reload it. (The suggested file name was U2-Plural
.)
-
Abstraction: Make a block that “specializes” on just one category, words that end with the letter “h”.
-
Start with a
list
of the words you want it to work for. -
This specialist block should work correctly for words that fit its specialty , . It can be completely wrong about words that don’t fit its specialty because the
plural
block should never give words toplural-h
unless those words end with “h”. -
Test with a variety of words to make sure it works the way you want. Then use
map
to test it on the entire list you made earlier.Click for a hint about creating a test list of words that begin with “h”.
Because you know what does, you can use its output as input to a test of
plural
orplural-h
:
-
Start with a
-
When you trust your new specialist block, edit
plural
to use the specialist. Test (you can usemap
) to make sureplural
still works for all the words it used to work for, as well as the new ones. -
Create
plural-y
to handle words like and
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.
Remember, any specialist can make mistakes if it’s asked to do a job that isn’t its specialty. For example:
Make sure plural
gives plural-y
only words it knows how to handle correctly.
I’m not thrilled with the way this pushes a particular implementation for a reason that’s hidden until they get there, and doesn’t make sense on its own. bh
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
-
Extend
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 editplural
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? -
Does your
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.-
Start by making a list of key-value pairs, with last letters as the keys and specialist procedures as the values:
To make the second item of each small list, find the block you want, put it in the second input to
list
, then right-click on the block and choose “ringify.” As the “x” example shows, it doesn’t have to be a block namedplural-
something. It just has to have an empty input slot where you want the word to go. -
Then in your
plural
procedure, you can replace most of theif
blocks with
We haven’t taught FIND FIRST yet. So I looked at 3.3.3 where we first use it, and we don’t teach it there either. :/ We actually teach it in 5.3.4. So I dragged some of that content back into U3 and also here. –MF, 1/11/21
Find first
is a higher-order function works similarly tokeep
, but it reports only the first item that’s found. It is equivalent toitem (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. Thecall
block’s first input slot is the empty gray ring, which means thatcall
wants a block in that input slot. When you dragitem 2 of pair
into that slot, the ring remains around it. That’s usually what we want when usingcall
, but not this time. Right-click on theitem of
block and choose “unringify” from the menu.You can experiment with
call
to see what else you can do with it. What happens if you don’t unringify theitem
block? What happens if you leave out the “with inputs
” and the second input? -
There’s no reason the keys have to be single letters. Modify the code in
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 put
in the list, and then words ending “ch” would have “es” added. This mechanism is very general; it works even for special cases such as
(You’ll have to define theword
function that just reports its input; it’s needed because you can’t type letters directly into a ring.) Now you have a really unclutteredplural
block. And it’ll even work for a language other than English, if you make a different specialist list. -
If you modify the trailing-substring test so that instead of giving up if it can’t find the last letter of the word as a key, it goes one step further and looks for an empty key in the specialist list, and you add
to the list, then you can be sure that every possible word will match that, if nothing else, so you don’t need theif
any more. Yourplural
block can now be just one-line:report (call (item 2 of (longest trailing substring ...
. -
For an extra challenge, modify your trailing-substring test so that a plus sign in the key matches any vowel (it will serve as a wild-card character), so you can do this:
-