I'm trying to practice recursive and tail recursive functions using Scala, I've created a tail recursive function to sum the values in two lists. I'm trying to do the same with recursion too but the only way I could think of was to modify the parameter each time the method was called like tail recursion. Can you help me out?
def callTailRecursive(list1 : List[Int], list2 : List[Int]) : List[Int] = { def callHelper(list1 : List[Int], list2 : List[Int], outputList : List[Int]): List[Int] ={ if(!list1.isEmpty && !list2.isEmpty){ callHelper(list1.tail,list2.tail,outputList:+(list1.head + list2.head)) }else if(list1.isEmpty && !list2.isEmpty){ callHelper(list1,list2.tail,outputList:+(list2.head)) }else if(!list1.isEmpty && list2.isEmpty){ callHelper(list1.tail,list2,outputList:+(list1.head)) }else{ outputList } } callHelper(list1,list2,List()) } def callRecursive(n : Int, list1 : List[Int], list2 : List[Int], outputList : List[Int]): List[Int] = { }