--- title: Drill 4 Solution, Fall 2020 tags: Drills-F20, 2020 --- # Drill 4 Solution, Fall 2020 ## Question 1 True or false: Functions can be passed as inputs to other functions. ( ) True ( ) False ::: spoiler Answer True This is possible because functions are values, just like Strings and Numbers. This is how filter-with and build-column are used! ::: ## Question 2 We want to call `filter-with` on a table `t` and a function `before-lunchtime`. Fill in the blanks in the definition of `before-lunchtime`: ``` fun before-lunchtime(r :: BLANK1) -> BLANK2: ... # body elided end ``` ::: spoiler Answer BLANK1: Row BLANK2: Boolean ::: ## Question 3 We use `build-column` on some table `t` as follows. Assume `build-new-col` is already defined. ``` build-column(t, "new-col", build-new-col) ``` What's the outcome of calling `build-column`? ( ) `t` is modified such that it includes the new column ( ) A new table is returned that includes the new column ::: spoiler Answer A new table is returned that includes the new column The value of a table can't be changed, just like the value of a Number or the value of a String. Instead, we can build new tables from existing tables. ::: ## Question 4 Assume we have some table `t` with a column named `"temperature"`. We use `filter-with` on `t` as follows. ``` filter-with(t, high-temperature) ``` If we wanted to include only the rows with a temperature greater than or equal to 500, what might be the *body* of `high-temperature` look like? Assume that the input to `high-temperature` is named `row`. ( ) `temperature >= 500` ( ) `row >= 500` ( ) `row[temperature] >= 500` ( ) `row["temperature"] >= 500` ( ) `"temperature" >= 500` :::spoiler Answer `row["temperature"] >= 500` ::: ## Question 5 Lets say we want to get a table that includes only pet owners who have more dogs than cats. We'll need a function ```more-dogs-than-cats```, which we can call with filter-by: ``` filter-with(pet-owners, more-dogs-than-cats) ``` The definition of more-dogs-than-cats looks like: ``` fun more-dogs-than-cats(r :: Row) -> Boolean: ... end ``` What could the body of more-dogs-than-cats be? [ ] `not(r["cats"] >= r["dogs"])` [ ] `not(r["dogs"] <= r["cats"])` [ ] `r["dogs"] > r["cats"]` [ ] `r["cats"] < r["dogs"]` :::spoiler Answer [X] `not(r["cats"] >= r["dogs"])` [X] `not(r["dogs"] <= r["cats"])` [X] `r["dogs"] > r["cats"]` [X] `r["cats"] < r["dogs"]` All of these could work. Make sure you can reason to yourself why all four are the same! However, the third and fourth choices are more conside and easier to understand, so it's best to go for one of those. :::