I have a function:
@tailrec
def sampleTailRec(list: List[Int]) : List[Int] = {
if(list.nonEmpty) {
val value: Int = list.head * 2
List(value) ++ sampleTailRec(list.drop(1))
} else {
List()
}
}
which is giving me following Compile error
could not optimize @tailrec annotated method sampleTailRec: it contains a recursive call not in tail position List(value) ++ sampleTailRec(list.drop(1))
I had tried to write code in Tail Recursion
Not able to understand why is my code not in Tail Recursion & how to make this method tail recursive?