title: scala n-arity tree tail recursive evaluation
body: <p>I have a Tree structure, which is more general than a binary tree structure</p>
  
  <pre><code>sealed trait Tree[+A]
  
  case class Leaf[A](value: Terminal[A]) extends Tree[A]
  case class Node[A](op: Function[A], branches: Tree[A]*) extends Tree[A]
  </code></pre>
  
  <p>As you see, it can have a arbitrary number of branches.</p>
  
  <p>I'm trying to make an evaluation method to be tail recursive but i'm  not being able to do it.</p>
  
  <pre><code>def evaluateTree[A](tree: Tree[A]): A = tree match {
    case Leaf(terminal) =&gt; terminal.value
    case Node(op: Function[A],  args @ _*) =&gt; op.operator((for (i &lt;- args) yield evaluateTree(i)))
  }
  </code></pre>
  
  <p>How can i save the stack manually?</p>