4.6) Write a function that calculates the difference between a pair of 2d points (x and y) and returns the result as a point. Hint: this would be a good use for tuples ([tuples_section]).
Answer
A 2-sized tuple is a good parameter type for specifying a point in 2d space. It’s components are unnamed, but we can imagine the first component as corresponding to x (the horizontal value) and its second as corresponding to y (the vertical value).
7) Write a function that takes a 2-sized tuple and returns it with the Int value (if included) in the first position. Hint: this would be a good use for type parameters and the isInstanceOf type operation.
Answer
This question should have stirred you to consider how you may change the type signature of a given tuple, and how changing the ordering of types in a tuple affects the return type of a function. Ultimately the resulting function must be non-type-safe, as the caller cannot expect to know in which order the types and values of a given tuple will be returned. Hopefully this solution is useful for learning the language, as it isn’t really a recommended solution for actual coding (due to the lack of type safety).
4.8) Write a function that takes a 3-sized tuple and returns a 6-sized tuple, with each original parameter followed by its String representation. For example, invoking the function with (true, 22.25, "yes") should return (true, "true", 22.5, "22.5", "yes", "yes"). Can you ensure that tuples of all possible types are compatible with your function? When you invoke this function, can you do so with explicit types not only in the function result but in the value that you use to store the result?
Answer
The phrase "tuples of all possible types" indicates that we’ll need to have type parameters for the input tuple. We can reuse them for the return tuple’s tupe, since the return tuple should keep the same ordering of input types.
Let’s invoke it and store the result in a value of an explicit type. The return type is based on interpolating the input tuple’s types with String types so this should be easy to figure out.
scala> val t: (Int,String,Char,String,Boolean,String) = stringify( (1, 'c', true) )
5.1) Write a function literal that takes two integers and returns the higher number. Then write a higher-order function that takes a 3-sized tuple of integers plus this function literal, and uses it to return the maximum value in the tuple.
Answer
The answer to the first part of the question is to write a lambda expression, i.e. a function literal. If you can write this answer then you’ll understand lambda expressions and function literals.
scala> val max = (x: Int, y: Int) => if (x > y) x else y
max: (Int, Int) => Int = <function2>
scala> max(23, 32)
res0: Int = 32
The second part of the question should give you pause. After all, how can you use a two-input function literal to compare three numbers? Hopefully you were able to figure out something like this:
5.2) The library function util.Random.nextInt returns a random integer. Use it to invoke the "max" function with two random integers plus a function that returns the larger of two given integers. Do the same with a function that returns the smaller of two given integers, and then a function that returns the second integer every time.
Answer
This should say "with three random integers", as the answer to the previous question was a function with a three-sized tuple and a comparison function. The max function is the function literal that takes two input numbers.
First, here’s the "max()" function literal invoked with two random numbers.