Displaying nested arithmetic expressions as a string using Scala pattern matching
You're right.  That repeated code in nestOperations() can be eliminated.

def nestOperations(es: Expr*): String =
  es.map {
    case Number(n) =&gt; n.toString
    case Prod(x, y) =&gt; nestOperations(x, y)
    case Var(s) =&gt; s
    case Sum(x, y) =&gt; "(" + show(x) + " + " + show(y) + ")"
  }.mkString(" * ")


But why stop there?  You can pull much the same stunt with show() and eliminate nestOperations() altogether.

def show(es: Expr*): String = es.map{
  case Number(n) =&gt; n.toString
  case Sum(e1, e2) =&gt; if (es.length &gt; 1) "(" + show(e1) + " + " + show(e2) + ")"
                      else show(e1) + " + " + show(e2)
  case Prod(e1, e2) =&gt; show(e1, e2)
  case Var(s) =&gt; s
}.mkString(" * ")