Transforming Every List Item

While working on the SIGCSE paper, Paul and I agreed that this is still a complicated way to introduce map (namely, in the context of ADTs in a project that's already known to be difficult). I think we need a simpler introduction--perhaps in the Sentence Builder content that will be added to the Gossip lab (e.g., plurals using map). (If we do this, the reference on 3.2.2 needs an edit.) --MF, 8/31/18
Other Random Ideas:

On this page, you will use your ADTs with the map block to display all the names in your contact list.

I think some version of the score adjustment question in the same items bank in the 2017 framework #22 on page 101 would be great. It's a program to add 5 to every score in a grade list unless that would make a score go over 100, then it's just 100. The students have to select the two responses (it's multiple-multiple choice) that perform the adjustment on each item. (They are given max and min functions.) I'd like to do a problem like this, where there is an adjustment being made to a list except in certain cases, but I don't want to copy this one. Thoughts on a different example? The best I can come up with right now is to adjust by a different amount and write our question without looking at the exam item. (Incidentally, and I DON'T want to teach it here prematurely, I wonder if we use the one-line IF in a HOF anywhere in BJC... U8? It could be presented as a simplification of this familiar problem...) --MF, 3/4/19

Also, I want to make sure we cover mutators well enough in shopping list/contact list because mutators, like them or not, are on the exam. --MF, 3/4/19

PG: I'd be happier if something happened with the result of #3 other than students having done it. Somehow this feels like an almost-lesson.

BH: Ditto. (I still want given and family names! We can make it doable.)

MF: This can't be the first place we introduce MAP

As you know, the keep block allows you to check every item in a list using a predicate, and it reports only the items that make the predicate true. The map() over () block also lets you work across a whole list at once. Map allows you to perform the same function on every item of a list. Map takes two inputs: a function (a reporter with a blank input slot) and a list, and it reports a new list. Every item in this new list is the result of calling the function with an item from the original list as input. For example:
map (join( )(s)) over (list {cat, dog, bird}) reporting {cats, dogs, birds}
map (round ( )) over (list{3.14159, 87, 5.4}) reporting {3, 87, 5}

You write the function that describes the change, and map applies that function to each item in the input list and then reports the list of values reported by the function.

Notice that the function mapped over the list always has a blank input slot. With both map and keep that blank is required. This blank is where the list item goes each time the function is performed.

  1. If it isn't open already, open your U3L1-ContactList project.
  2. Talk with Your Partner Experiment with these examples of map. Discuss and then explain in writing what these expressions are doing.
    map (join( )(s)) over (list{block, script, Boolean})
    map ((3) * ( )) over (list{7, 8, 1})
    map (letter (1) of ( )) over (list{bounce, join, clear})
  3. Use the map block together with your selectors to report a list of only the names of all contacts.

Self-Check: Map Block

  1. This question refers to these two lists:
    set (words and numbers) to {rabbit, 5, benefit, is, 34, kite, 2, 305, the, 61}
    set (capitals) to {{Augusta, Maine}, {Boise, Idaho}, {Columbia, South Carolina}, {Des Moines, Iowa}}
    Which of these statements are true?
    Choose all that apply.

    map (letter (1) of ()) over (words and numbers) reports the list {r, 5, b, i, 3, k, 2, 3, t, 6}.
    map (item (1) of ()) over (words and numbers) reports the list {rabbit}.
    map (item (1) of ()) over (capitals) reports the list {Augusta, Boise, Columbia, Des Moines}.
    map (letter (1) of ()) over (capitals) reports the list {A, B, C, D}.
    map (all but first of ()) over (capitals) reports the list {Maine, Idaho, South Carolina, Iowa}.
  2. Consider the non-abstracted code on the left and also the abstracted code on the right. Write Out Your Thoughts Give two reasons why the use of abstraction in the second example is preferred.
    script variables (names of contacts), set (names of contacts) to (list), for each (contact) of (contacts list): (add (name from contact: (contact)) to (names of contacts)), report (names of contacts)
    map (name from contact: ()) over (contacts list)