Processing Student Grades

There is commented out text on this page. --MF, 6/11/21

On this page, you will use higher-order functions to perform various operations on student grades: averaging, scaling, and selecting students within certain grade ranges.

Making a Variable

  1. Choose make a variable buttonfrom the Variables palette.
  2. Type the title for your variable: student grades.
  1. First, create a new variable called student grades and set its value to a list of various student grades.
    set (student grades) to (list {84, 73, 95, 82, 76, 89, 83, 79, 96, 62, 86, 69, 94})

Selecting within a Range with Keep

Predicates are reporters that always report a Boolean value (they always report either true or false). Predicates are the condition used by conditionals (such as if block or repeat until) to decide when to do something. In snap, predicates are hexagonal in shape, not oval like other reporters.

The keep items 'predicate input slot' from 'list input slot' block takes a predicate (with a blank input slot) and a list as input, and it creates a new list keeping only those items from the input list that match the condition defined by the predicate. For example:
keep items such that (is () a (number)?) from {5, :), six, 7, elephant, 3} reporting {5,7,3}
keep items such that ((letter (1) of ()) = (a)) from {Ann, Olivia, Jamal, Karl, A.J., Tara} reporting {Ann, A.J.}

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.

Import Tools
  1. Imagine that you want to know how many students scored above 85. Use keep to report only the student grades that are over 85.
  2. Create a scores above reporter block that accepts a number and a list of scores as input and reports the scores that are over the inputed number.
    scores above (85) in (student grades) reporting {95, 89, 96, 86, 94}
  1. Find the outliers. Use the or block together with keep to report the student grades that are either over 90 or under 70.

Scaling with Map

The map 'reporter input slot' over 'list input slot' block takes two inputs: a function (a reporter with a blank input slot) and a list, and it reports a new list. In this new list, each item is the result of running an item from the original input list through the function. For example:
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 then reports the list of values changed by the function.

  1. Imagine that you realize that your test was too hard and you want to add 4 points to every student's grade. Use the map block to find the list of adjusted grades.
  2. Create a scale by reporter block that adds the input number to every item in a list.
    scale by (4) in (student grades) reporting {88,77,99,86,80,93,87,83,100,66,90,73,98}

Averaging with Combine

These COMBINE example images are out of date. --MF, 6/11/21

The combine 'list input slot' using 'reporter input slot' block takes an operation (with two blank input slots) and a list as input, and it reports a single result (not a list): the combination of the elements in the list when using the given operation. For example:
combine with (()+()) items of (list {5,6,2,3}) reporting 16
combine with (join words () ()) items of (list {apples, bananas, oranges, grapes}) reporting 'apples bananas oranges grapes'

You choose the operation (with two input slots), and combine performs that operation combining all the items in the input list and reports the result.

  1. Talk with Your Partner How could you use the combine block to help you build a block that will find the average of the student scores? What other blocks will you need?
  2. You will need the red length of list block which can be found in the Variables palette (not the green length of operator block).
  3. Create an average block in Snap! that will accept a list as input and output the average of the items in the list. Try it out with your student grades list.
    average (student grades) reporting 82.15...
 
  1. Find students who are closest to the class average. Use the and block together with average and keep to report the student grades that are within 5 points of the class average.

For a somewhat more challenging introduction to higher-order functions in a game context, see this page.