Teacher's Choice

The Map Block (Teacher's Choice)

The map block takes two inputs: a function (with a blank input slot) and a list, and it reports a new list, in which each item is the result of running an item of the input list through the function. For example:
map (( )-(1)) over (list (96) (-100) (4.7)) reporting
map (join ( ) (s)) over (list (cat) (dog) (bird)) reporting
map (round ( )) over (list (3.14159) (87) (5.4)) reporting

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

The gray ring in map means that the input should be a function. The function can be numeric as in algebra, for example: (3 * ()) + 7 (as we'll explore later) or () - 1 (as shown above), but you can also use other operators like join () (s): the map function above joins each word with the letter 's'. The blank input slot in the function is filled with each list item to make the new list. In algebraic functions, that's the variable, for instance: that's the x in the function f defined by f(x) =3x + 7.

Transposing Music

The simplest bits of a scales project could also work. Two scales (I'd recommend major and double-harmonic only because they give cool variety and, btw, validate an interesting group of kids), a transposer (map), and something that plays them (for-each). --PG
    Set Up Your Headphones or Speakers
  1. Talk with Your Partner Discuss what map does in this script: for each (note) of (map (_ + 5) over (list (60) (64) (67) (72) (60))) (play note (note) for (1) beats)
  2. Open this project. It contains two musical scales (a major scale and an Egyptian scale) and a song (Twinkle Twinkle Little Star). Each is just a sound reporter that reports a list of notes.
  3. Why isn't transpose a reporter?
  4. Create a block that takes a list of note pitches and a shift number as input and plays each note on the list mapped up or down as many pitches as the input indicates. For example, this script will play the notes {65, 69, 72, 77, 65}:
    You may wish to set the tempo to something like 240.
  5. Try your transpose by block with several different shift numbers so that you can hear the impact of map.
  6. Use your transpose by block to hear the impact of map on each of the two scales and the song. For example, try:
    transpose(twinkle twinkle little star) by (-2) and play
Could pull ideas from Old Looks and Sound Page
I think this cannot be done with map... -MF

Now, you'll transpose songs, which have lists of lists of note pitches and beats.

  1. Open your U3L1-PlaySong project from Lab 1.
  2. Create a reporter block that transposes a list of sounds, where each list item is a list with the sound's note and duration. Your block should take as input a list with the song data and a number indicating how much to transpose the song.
    map will be helpful here.
    transpose {} by ()

  3. Musical scales are sets of notes ordered by pitch. For example, this is the C Major scale and the Snap! note numbers:
    CDEFGAB
    60626465676971
  4. Create a block that plays a scale.
  5. Create a reporter that generates an arpeggio of a scale...

Mapping Plurals into Sentence Builder

  1. Open "U3L1-SentenceBuilder" from Lab 1.
  2. Modify your noun phrase block so that half of the time it uses a plural noun.
  3. Note that when you use a plural noun, the article can not be "a" because "a" implies that it is a singular noun. Devise a solution.

Drawing 3D Shapes

Lab1's last page, draw shape, has lots of numbers, but we're doing something with them. If we used MAP over the vertices of A to translate it, or dilate it, that would be ok. I actually thought that was here somewhere, but couldn't find it. That would require a slightly more complex "getting inside" the list operation, but the kids in their dialogue have already begun that thinking in draw shape. But when we get to the page on MAP, it applies arbitrary functions to arbitrary numbers for no apparent reason other than exercise.
Could you invent a MAP page that builds on draw shape, maybe translating or reflecting or dilating? You could exchange x and y coords and see what happens. A translation of, say, (20, 10) could look 3-D ish, especially if corners of the two were connected. So would a connecting an image and a dilation. --PG

You can use map to translate coordinates on a plane.

  1. Open your "U3L2-DrawShape" project from Lab 2.
  2. Use addition to translate the shape to different locations.
  3. Create a shape zoo block that uses your draw shape block together with map to draw the shape (given as an input list) at random places on the stage.
  4. "U3L3-ShapeZoo"Save your work as U3L3-ShapeZoo
    Use multiplication to dilate the shape.
  5. Change your shape zoo block so that it dilates (re-scales) each shape by a random factor (for example, from .5 to 3).
  6. Talk with Your Partner What changes about a coordinate pair when it flips across the x- or y-axis?
  7. Adjust your shape zoo block so that it sometimes reflects (flips) the shape vertically and/or horizontally.
I found the 3D thing too difficult, and I have not gotten it working yet, so I made it a TIF. Someone please check that it's reasonable to ask this? --MF
 
  1. Create a draw 3D shape block that takes a list of points as input and does the following:
    1. Draws the shape by calling your existing draw shape block
    2. Draws a second copy of the shape shifted 20 in the x direction and 10 in the y direction
    3. Connects the corresponding vertices (corners) of the two copies of the shape
  2. Describe the result of this block:
    map over coordinates
      You drew the letter A in The For Each Block.
    1. Build a block move that moves the letter A 37 units left and 23 units up.
    2. Make move more general, so that you can input how much you want to change each coordinate of the elements of A.
    You can connect corresponding vertices for a 3-D effect.

Using Map for Algebraic Functions

Alphie and Betty are talking about another example of using map to compute some function of every item of a list:
Betty: That gray ring after the word "map" means a function goes in there. A function? Like in algebra? What about the function f defined by f(n) = 3n + 7?
Alphie: Well, I don't think the function has to be mathematical. We used plural with words. But let's try to put the function f in there. First I need to build f. Here you go:
f(n)=3n+7
Then we map f over a list, like 2, 0, and 7:
f(n)=3n+7
and it works!
Betty: But I think I see an easier way. There's no need to name f. We leave the input in the expression blank, just like we did in join.
Betty makes and clicks to see map 3x+7
Alphie: Let's see. 2 times 3 is 6. And then add 7. That's 13. Good, it worked!
  1. "U3L3-FunctionPractice"Create a new project called U3L3-FunctionPractice
  2. Import Tools
  3. Practice: Write a block for the following function: g(n)=6n+5
  4. For each of these problems, start with this block: map over list(2,0,7)
    1. Write a function for map that would report the list 3, 1, 8.
    2. Write a function for map that would report the list 4, 0, 14.
 
  1. Using the same input list as above, {2, 0, 7}, write a function for map that would report the list 4, 0, 49.
  2. The TiF is also pretty arbitrary. We're getting some mathematical lesson in comparing iii and iv, but we're not getting CS, and it's not interesting. --PG?
    Describe what each of these does to the numbers in the list {4,3,2,7}
    Write an algebraic expression for what each one does.
    1. map +2 over (map*3 over {4327})
    2. map *3 over (map + over {4327})
    3. map +2*3 (over {4327})
    4. map *3+2 (over {4327})
    5. map _+_ over {4327}
    6. map _+_ over _*_ {4327}
  3. Talk with Your Partner Explain what map is putting in the blank spaces here: map (join (3blanks)) over nouns
  4. Build a reporter compose that takes two functions and a list as input and reports the the result of mapping the first function over the result of mapping the second function over the list:
    map 3x+7
JK: Comment from Dan and NY teachers: "They worked for quite a while, not realizing that if the function passed to map is "item _ of LIST" that they could remove the 1 to leave a blank slot. This has come up before with students who see a drop-down menu and don't realize it can be edited."

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}.