Question
SCALA PLEASE Write a tail recursive function extractSubList that inputs three arguments lst of integers, index i and length j. Given a list of integers
SCALA PLEASE
Write a tail recursive function extractSubList that inputs three arguments lst of integers, index i and length j. Given a list of integers lst, an index i into the list and a length j, extract all the elements in the original list lst(i), ..., lst(i+j-1) into a new list. Handle corner cases by returning an empty list when (a) j <= 0 or (b) i+j is greater than the length of the list lst.
Hint: Write a function extractSubList that handles all the corner cases above and calls a "helper" function extractSubListHelper for all the non-corner cases. Make sure that extractSubListHelper is itself tail recursive. Make sure that the @tailrec decorator is applied to the helper function.
Restrictions:
Your helper function must be tail recursive.
You can use list.head and list.tail functions
You can use list.length.
You can use list cons operator elt::list to append an element in front of a list, and also use :+ operator to append to the back of the list. You can also use ++ operator (or ::: operator) to append two lists to each other.
You can reverse a list by calling the List API function list.reverse.
You cannot access list elements using their indices: Eg., lst(i) to directly access the i-th element is forbidden.
No other list API functions allowed.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started