Scala – List – concatenation (:::)

Concatenation (::: ) is implemented as method in class List

The result of xs ::: ys is a new list that contains all the elements of xs, followed by all the elements of ys.

Like cons, list concatenation associates to the right

xs ::: ys ::: zs
is interpreted like this:
xs ::: (ys ::: zs)

 

object List_Concatenation extends App{

  val l1 =List(1,2,3)
  val l2=List(3,4,5)
  val l3=l1:::l2

  println(l3)
  println(l3:::Nil)

  println(List():::l3)
  println(List(1,2,3):::List(4))
}

Leave a comment