Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

Students also viewed these Databases questions

Question

How can the Internet be helpful in a job search? (Objective 2)

Answered: 1 week ago