Write a recursive method sum that takes a
List<Integer> and returns the int sum of
all the numbers in it. The sum of an empty list is 0.
Write a recursive method product that takes a
List<Integer> and returns the int
product of all the numbers in it. The product of an empty list is
1.
Write a recursive method max that takes a non-empty
List<Integer> and returns the largest number in it.
The base case is a list with one element; for a longer list, the answer
is the larger of the first element and the max of the rest.
Write a recursive method contains that takes a
List<Integer> and an int
target and returns a boolean indicating
whether the target is in the list. An empty list contains nothing.
Write a recursive method count that takes a
List<Integer> and an int
target and returns the number of times the target appears
in the list.
Write a recursive method allEven that takes a
List<Integer> and returns a boolean
indicating whether all the numbers in the list are even. All the numbers
in an empty list are even.
Write a recursive method someEven that takes a
List<Integer> and returns a boolean
indicating whether some (i.e. at least one) of the numbers in the list
are even.
Recursion over lists. Every method here takes a
List<Integer> and is written the same way: a base case
for the empty list (or the one-element list, where the empty list has no
answer), and a recursive case that does something with the first element
and the answer for the rest of the list. The starter code provides two
helpers for taking a list apart: first(list) returns the first
element and rest(list) returns a view of everything after it.
Note that rest doesn’t copy the list, so calling it is cheap,
but changing the view changes the original list. Neither helper can be
called on an empty list.
No loops and no streams; the point is the recursion. Also no reaching for the library method that already does the job.