Starting from:

$30

HW 6: Scala, lists, currying


HW 6: Scala, lists, currying
 Points 25 Submitting a file upload
1. (5 points) Write a function reduce that takes in two parameters: a function f that takes in two Ints
and returns an Int, and a list xs of Ints. You should use the function f to combine all of the
elements of the list. For instance, if I have a list and a function as follows:
def ls = 7::2::5::1::Nil
def add(x:Int, y:Int) = x+y
then reduce(add, ls) would return 7+2+5+1= 15. Ensure that your function would also do the
right thing if called with a function that
multiplied two inputs.
2. (5 points) Write a function in Scala whose parameters are: two lists of Ints, xs and ys, and a
function f that takes in two Ints and returns an Int. The function should return a new list whose
elements are obtained by applying f to the corresponding elements of xs and ys. Test your
function using anonymous functions corresponding to f(x, y) = x+y and f(x, y) = x*x+y. For
example, if xs was the list 3, 8, 1, 5 and ys was 12, 6, 23, 1, 8, 4 and the f used was f(x, y) = x+y,
our result list would be 15, 14, 24, 6. Note that if one list is longer than the other, the extra
elements are not included in the result list. You may NOT use a helper function with extra
parameters.
3. (5 points) Write a curried version of your reduce function from problem 1. Your function should
take in a function and return another function which takes in a list of Ints and returns an Int. You may
return an anonymous function or a named one.
4. (5 points) Write a curried version of your function from problem 2, which takes in the function f and
returns another function that takes in two lists of Ints and returns a list of Ints. You may return an
anonymous function or a named one. You may NOT use a helper function with extra parameters.
5. (5 points) Write a curried version of your function from problem 4. In this part, you will curry the
inner (returned) function from problem 4, so that the inner function takes in a list of Ints, and returns a
function that takes in a second list of ints and returns a third list of Ints. You may return an anonymous
function or a named one. You may NOT use a helper function with extra parameters.
You can submit your functions for problems 1-5 in one file called HW6-1-5-Lastname.scala

More products