@Brian's answer is nice but it reverses the input list. That's generally not the intended behaviour.
@jwvh's recommendation is pass the accumulator in a 3rd parameter to the function but that leaks private API to public API.
Either solution would necessitate reversing the accumulator before returning the answer – effectively iterating thru your input list twice. That's an insane implementation, especially considering you're trying to implement this to facilitate large lists.
Consider this tail-recursive implementation which does not expose private API and does not require the accumulator to be reversed after filtering.
disclaimer: this is the first scala procedure I have ever written. Feedback on any implementation style or detail is welcomed.
def filter(n: Int, xs: List[Int]): List[Int] = {
@scala.annotation.tailrec
def aux(k: List[Int] => List[Int], xs: List[Int]): List[Int] = xs match {
case Nil => k(Nil)
case x :: xs if (x < n) => aux(k, xs)
case x :: xs => aux((rest: List[Int]) => k(x :: rest), xs)
}
aux(identity, xs)
}
filter(5, List(1,2,3,4,5,6,7,8,9,0)))
// => List(5, 6, 7, 8, 9)
terminal
value or make a call to itself. Brian has already posted a good example. In your snippet, you call a cons constructor which doesn't satisfy the condition of a tail recursivity. The reason why tail recursive function doesn't cause stack overflow is because it is converted to a loop. – maks