Idiomatic Scala solution tailrec


#1
  def evalRPN(A: Array[String]): Int = {

    def loop(i: Int, stack: List[Int]): Int =
      if (i == A.length) stack.head
      else A(i) match {
        case "+" => loop(i + 1, (stack.tail.head + stack.head) :: stack.tail.tail)
        case "-" => loop(i + 1, (stack.tail.head - stack.head) :: stack.tail.tail)
        case "*" => loop(i + 1, (stack.tail.head * stack.head) :: stack.tail.tail)
        case "/" => loop(i + 1, (stack.tail.head / stack.head) :: stack.tail.tail)
        case number => loop(i + 1, number.toInt :: stack)
      }

    loop(0, List.empty[Int])
  }