title: Scala: recursively adding to immutable map?
body: <p>How can I add to immutable map without using vars? How to <strong><em>bind</em></strong> immutable to a new value? When trying the following code I get an error:</p>
  
  <pre><code>"reassignment to val"
  </code></pre>
  
  <p>my code:</p>
  
  <pre><code>object Play {
  
    def main(args: Array[String]): Unit = {
            test1()
    }
  
    def test1() {
  val pairs = Array(("a", 1), ("b", 2), ("c", 3), ("d", 4))
  val m = Map[String, Int]()
  
  for (x &lt;- pairs) {
      m = addToMap (x, m)
  }
    }
  
    def addToMap(pair: (String, Int), map: Map[String, Int]): Map[String, Int] = {
        map + (pair._1 -&gt; pair._2)
    }
  
  }
  </code></pre>