--- title: Drill 3 Solutions, Fall 2020 tags: Drills-F20, 2020 --- # Drill 3 Solutions, Fall 2020 ## Question 1 Suppose we have three functions defined as follows: ``` f1(row :: Row) -> Boolean f2(table :: Table) -> Row f3(row :: Row) -> Row ``` Which of the following are valid ways to combine the functions? Assume t is a table and r is a row. [ ] f3(f1( r )) [ ] f1(f3( r )) [ ] f1(f3(f2( t ))) [ ] f2(f3(f1( r ))) [ ] f2(f3( r )) ::: spoiler Answer [ ] f3(f1( r )) [X] f1(f3( r )) [X] f1(f3(f2( t ))) [ ] f2(f3(f1( r ))) [ ] f2(f3( r )) A good strategy for thinking about these is to read them from the inside-out. Does the most inner function have the correct input type? Does its output type have the correct input type for the second most inner function? ::: ## Question 2 Assume we have a table named results with a column named score.  You try the following expression to sort the table from low to high score. What will Pyret produce? ``` sort-by(results, score, true) ``` ( ) a table sorted low to high by the score ( ) a table sorted high to low by the score ( ) an error about the score column input ( ) an error about the table input ::: spoiler Explanation an error about the score column input The input table is fine as written, but the call to sort-by will fail because column names must be strings, so the input has to be written as "score" rather than just score. Otherwise, Pyret looks in the known names list for a string associated with the name score. Also note that, if this call didn't error, the third input being true signals low to high, not high to low! ::: ## Question 3 Why might someone write a helper function? ( ) Reduce repeated code ( ) Make code more easily testable ( ) Make code more readable ( ) All of the above ::: spoiler Answer All of the above ::: ## Question 4 Suppose you're writing code to generate flags with three horizontal stripes, and you decide to create a helper function, `draw-three-horizontal-stripes`, to reduce the amount of repeated code. What might this function take as input? [ ] Name of the country (String) [ ] Color of the topmost stripe (Color) [ ] Whether the flag has stripes (Boolean) [ ] Color of the middle stripe (Color) [ ] Image of the flag (Image) [ ] Color of the bottom stripe (Color) :::spoiler [ ] Name of the country (String) [X] Color of the topmost stripe (Color) [ ] Whether the flag has stripes (Boolean) [X] Color of the middle stripe (Color) [ ] Image of the flag (Image) [X] Color of the bottom stripe (Color) Name of the country (String): This parameter would likely not be used to draw the flag. It's best practice to avoid unused parameters. Whether the flag has stripes (Boolean): The name of the function indicates that it draws flags with three horizontal stripes. It's best practice to limit the scope of helper functions, and having alternate logic in case the flag didn't have three stripes would exceed the stated purpose of the function. Image of the flag (Image): It's the function's job to create this image. ::: ## Question 5 Suppose we have a function with the following header. Which inputs might be valid? Assume we have a table named `restaurant-data` with columns named "name" and "price" and a row with "name"="Baja's" and "price"=10. ``` fun cheap-restaurant(row :: Row) -> Boolean ``` [ ] restaurant-data [ ] "Baja's" [ ] "price" [ ] restaurant-data[0] [ ] 0 [ ] restaurant-data.row-n(0) [ ] get-row(restaurant-data, 0) [ ] None of these are valid :::spoiler [ ] restaurant-data [ ] "Baja's" [ ] "price" [ ] restaurant-data[0] [ ] 0 [X] restaurant-data.row-n(0) [X] get-row(restaurant-data, 0) [ ] None of these are valid The type of the input needs to be Row. The types of the answers, in order, were Table, String, String, invalid, Row, and Row. ::: ## Question 6 Given this table: ![](https://i.imgur.com/mlqoy3U.png) Does this table contain 3 rows? ( ) True ( ) False :::spoiler Explanation False This table has 2 rows because the table header is not a row of data. ::: ## Question 7 Is this helper function necessary? ``` fun sort-table(t :: Table, col-name :: String) -> Table: sort-by(t, col-name, false) end ``` ( ) True ( ) False :::spoiler Explanation False In general, helper functions that exist solely to call a built-in function are unnecessary. Instead of writing and testing a helper, you should just call the built-in function in your code. :::