title: Copy elements from one list to another in Scala
body: <p>I'm would like to create generic (invariant) method in Scala which copies elements from source list to destination list. In Java there is <strong>copy</strong> method in <strong>java.util.Collections</strong> (see <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#copy%28java.util.List,%20java.util.List%29" rel="nofollow">http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#copy%28java.util.List,%20java.util.List%29</a>). I know in Scala List is immutable object so i would like to create and return new list.</p>
  
  <p>I've written the following code:</p>
  
  <pre><code>def copy[T](dest:List[T], src:List[T]):List[T] = {
      if(dest.length&lt;src.length) throw new Exception("IndexOutOfBoundsException")
      else if(src==Nil) dest
      else {
          var ret = dest
          ret = dest.updated(0, src.first)
          var i=1
          val f:(T=&gt;Unit) = a =&gt; {
              if(i&lt;src.length) ret=ret.updated(i, src(i))
              i+=1
              ()
          }
          dest.foreach(f)
          ret
      } 
  }
  </code></pre>
  
  <p>But I think it could be written better. Could you help me to write better code? Thanks in advance.</p>
  
  <p><strong>EDITED</strong>: Maybe I expressed unclear what I want to do. I have two lists (scala.collection.immutable.List), e.g. <strong>src</strong> (length=x) and <strong>dest</strong>(length=y>=x). I would like to replace first <strong>x</strong> elements of <strong>dest</strong> list with elements from <strong>src</strong> list.</p>