Something like this?
def fromList(list1: List[Int]): Int = list1.reverse.fold(0)(10 * _ + _) fromList(List(2,3,6)) //res0: Int = 632
Can also be done with foldRight().
def fromList(list1: List[Int]): Int = list1.foldRight(0)(_ + _ * 10)
If recursion is required...
@annotation.tailrec def fromList(list1: List[Int], acc:Int = 0): Int = if (list1.isEmpty) acc else fromList(list1.init, 10*acc + list1.last) fromList(List(2,3,6,1)) //res0: Int = 1632
Or the slightly more verbose but likely more efficient...
def fromList(list1: List[Int]): Int = { @annotation.tailrec def loop(lst: List[Int], acc:Int = 0):Int = lst match { case hd::tl => loop(tl, 10*acc + hd) case _ => acc } loop(list1.reverse, 0) } fromList(List(7,3,6,4)) //res0: Int = 4637
There are 2 major problems with your original design.
1st - Both list1.head and list1.tail are impossible if list1.isEmpty. So you need to test for that before decomposing the List.
2nd - You need to adjust the intermediate results before adding the current digit.
//recursive but not tail-recursive def fromList(list1: List[Int]): Int = if (list1.isEmpty) 0 else 10 * fromList(list1.tail) + list1.head