Students build on their knowledge of the image-scatter-plot function, motivating the need for if-expressions in their programming toolkit. This drives deeper insight into subgroups within a population, and motivates the need for more advanced analysis.

Lesson Goals

Students will be able to…​

• use if-then-else expressions in Pyret

• explain the behavior of a (specific) higher order function

Student-facing Lesson Goals

• Let’s explore functions that behave differently based on the input.

Materials

Preparation

• Make sure all materials have been gathered

• Decide how students will be grouped in pairs

• Computer for each student (or pair), with access to the internet

• Student workbook, and something to write with

• All students should log into CPO and open the "Animals Starter File" they saved from the prior lesson. If they don’t have the file, they can open a new one

Supplemental Resources

Language Table

 Types Functions Values Number `+`, `-`, `*`, `/`, `num-sqrt` `4`, `-1.2`, `2/3`, `pi` String `string-length`, `string-repeat`, `string-contains` `"hello"`, `"91"` Boolean `<`, `<>`, `<=`, `>=`, `<`, `>`, `==`, `<>`, `>=` `true`, `false` Image `star`, `triangle`, `circle`, `square`, `rhombus`, `ellipse`, `regular-polygon`, `radial-star`, `bar-chart`, `pie-chart`, `box-plot`, `scatter-plot`, `bar-chart-summarized`, `pie-chart-summarized` `🔵🔺🔶` Table `.row-n`, `.order-by`, `.filter`, `.build-column`

## 🔗Warmup

1. Show students this code, which uses `image-url` and `scale` to generate icons of animals.

2. What do they Notice? What do they Wonder? How might this scatterplot change our analysis?

3. Have students make a scatter plot of animals, using `age` as the x-axis values and `weeks` as the y-axis.

(For now, the scatter plot is purely to give students practice with contracts and displays. They are not expected to know much about scatter plots at this point.)

## 🔗If-Expressions 20 minutes

### Overview

Students explore a program that makes use of an if-expression, develop their own understanding, and modify it.

### Launch

So far, all of the functions we know how to write have had a single rule. The rule for `gt` was to take a number and make a solid, green triangle of that size. The rule for `bc` was to take a number and make a solid, blue circle of that size. The rule for `nametag` was to take a row and make an image of the animal’s name in purple letters.

What if we want to write functions that apply different rules, depending on the input? For example, what if we want to change the color of the nametag depending on the species of the animal?

### Synthesize

Have the class share their own explanations for how if-expressions work.

Pyret allows us to write if-expressions, which contain:

1. the keyword `if`, followed by a condition.

2. a colon (`:`), followed by a rule for what the function should do if the condition is `true`

3. an `else:`, followed by a rule for what to do if the condition is `false`

We can chain them together to create multiple rules, with the last `else:` being our fallback in case every other condition is `false`.

## 🔗Better Image Scatter Plots 20 minutes

### Overview

Students discover how "dot appearance" can be used to show more data in a scatterplot, and why that would be valuable.

### Launch

Suppose we want to make a scatter plot for the Animals Dataset, but with dots taking different colors depending on the species. This would make it possible to see if certain species are "clustered" in different parts of the plot.

### Investigate

Have students open Word Problem: species-color (Page 47). Make sure they all write the Contract and Purpose Statement first , and check in with their partner and the teacher before proceeding.

Once they’ve got the Contract and Purpose Statement, have them come up with `examples:` for each species. Once again, have them check with a partner and the teacher before finishing the page.

Once another student and the teacher has checked their work, have them type this function into their animals starter files, and use it to make an `image-scatter-plot` using `age` as the x-axis and `weeks` as the y-axis.