Assignment 3

Raffi Khatchadourian

October 5, 2020

Notes

Arguments vs. Parameters

The below questions refer to the terms “arguments” and “parameters.” As a review, the term argument refers to the value passed to a function or method, while the term parameter refers to the names listed in a function’s definition. Correspondingly, if you are asked to alter an argument, you are to alter the function invocation. Conversely, if you are asked to alter a parameter, you are to alter the function definition.

Questions

  1. Create a higher-order function called composite that takes two (Double) => Double functions and a double and applies the first function to the return value of the second function given the double argument.

  2. Import the Scala Math library. Then, apply the composite function to the arguments ceil, floor, and 5.5. Show the correct code on how to invoke composite using these arguments. What does composite return given these arguments?

  3. Rearrange the arguments so that composite returns 6.0. Show the code invoking composite to obtain this value.

  4. Create a higher-order function called composite that takes two (Double) => Double functions and returns a function that applies the first function parameter to the second function parameter.

  5. What is the type of this function?

  6. Store this function in a constant called compFunc. Show the code for how to do this.

  7. Compose ceil and floor using compFunc and store it in a constant called upAndDown. Show the code.

  8. Call upAndDown, giving 5.5. What value did you get? Explain how this works.

  9. Show the code to compose floor and ceil directly, i.e., without higher-order functions, again giving 5.5. Do you get the same answer?

  10. During lecture, we saw how to generate a sequence 0.1, 0.2, …, 0.9 using (1 to 9).map(0.1 * _). Write the code to make (0.1 to 0.9) work correctly by avoiding the map function.