Teacher's Choice

The Keep Block (Teacher's Choice)

The keep block takes a predicate (with a blank input slot) and a list as input and creates a new list keeping only those items from the input list that match some criterion, such as "begins with a vowel," "is less than five characters long," "is even," or "is a vegetable." For example:

Image blocks need alt and title attributes. --MF



You write the predicate that does the checking, and keep applies that predicate to each item in the input list and reports the list of elements that make your predicate true.

Keep is ok, but it could be augmented to foreshadow some other stuff. For example, if kids imported a data set (so that they did not have to create it themselves), they could keep all the points that are higher than whatever. Could be grades, but it'd be nice if they could actually answer some useful question by performing some list operation. Count the entire list; keep all the values that are over some level; count that list; compare? (not brilliant, come up with something better)
    Set Up Your Headphones or Speakers
  1. Talk with Your Partner Discuss what keep does in this script:

Filtering Data

  1. Import this data set of class grades.
  2. Count the number of items in the list.
  3. Use keep to create a filter that keeps only the values of a list that are over some input number. (It should be a reporter that takes a list and a number as input and reports a new list of all of the values in the original list that are greater than the specified number.)
  4. Count number of grades that are 80 or above.
  5. Talk with Your Partner Compare the size of the original list with the list of the scores of 80 or better.

Click on the script to open a new file.
Alabama, Alaska, ... Wisconsin, Wyoming

Notice that this gray ring's inner boundary is hexagonal, which means you should use a predicate function, which is a function that reports true or false.

Alphie and Betty discuss using keep: KEEP ITEMS SUCH THAT (<predicate>) FROM (list)

Alphie: Let's figure out how keep works. Which states do we want to keep?
Betty: Let's get a list of the states that begin and end with the same letter.
Alphie: So the list we're using is states. And letter 1 of the word needs to be the same as the last letter of the word.
Betty: Ok, so far we have this:
keep(letter_1=letter_blank)from(states)
Alphie: We want the last letter of each word. The last letter depends on the length of the word.
Betty: Oh, right, so we can do this:
keep(letter_1=letter(length))from(states)
Alphie: Each item of states goes in the empty spaces. So, let's try an example. I'll pick a state... "new york". Letter 1 of "new york" is "n".
The space in "new york" counts as a character. So "new york" has a length of 8 and the eighth letter is "k".
Betty: And that equals... Well, length of "new york" is 8, and letter 8 of "new york" is "k". The condition is false because "n" doesn't equal "k". So "new york" doesn't show up in the list. We'll keep only the states that have the same first and last letter.
    1. Define a block that will keep all the states that start with a given letter. Here's an example of the block in action:
      states with S -> south carolina, south dakota
    2. Write an expression that will select all the states whose names are at least seven letters long.

Using Map and Keep to Access a Database

A database is a list of records. Each record is a list of its own (the "data" for that record). For example, a database of people in New York might consist of records that contain first name, last name, address, and phone number. To be useful, a database needs selectors that pull out individual pieces of information from each record.

  1. Talk with Your Partner Discuss how map and keep might be useful in selecting and organizing data from a database.
  2. We could do a lot more here with abstraction. Should we?
    1. Make a database with data from a few people in your class. Each record contains first and last names, age (in years), and height (in inches). So, your database might start out like this:
      data_from_class
    2. Make your selectors. You need four of them; you could call them firstname, lastname, age, and height. Here's how age might work:
      age_sasha
    3. Search your database and create a list of all classmates who are...
      1. The same age as you
      2. Taller than you
      3. Younger than you
    4. Search your database and create a list of all classmates whose...
      1. Last name has the same first letter as yours
      2. First name starts with a letter that comes before yours in the alphabet
  1. Make a block that takes your database and reports a rearrangement of the records so that the last names of your classmates are in alphabetical order.

Use this list of words and numbers:
set (words and numbers) to {rabbit, 5, benefit, is, 34, kite, 2, 305, the, 61}
Which of these expressions produce the list with just the numbers: {5, 34, 2, 305, 61}?
Choose all that apply.

map (is () a (number)?) over (words and numbers)
keep items such that (is () a (number)?) from (words and numbers)