Singly-linked list/Element insertion: Difference between revisions
Content added Content deleted
m (+Stata) |
|||
Line 1,404: | Line 1,404: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
In Scala (and functional programming) we create a new list instead of modifying existing one. |
|||
Placing the method in a companion object (like a static method in Java) |
|||
<lang scala> |
<lang scala> |
||
/* |
|||
def insert(a: Node, c: Node) = { |
|||
Here is a basic list definition |
|||
c.next = a.next |
|||
a.next = c |
|||
sealed trait List[+A] |
|||
} |
|||
case class Cons[+A](head: A, tail: List[A]) extends List[A] |
|||
case object Nil extends List[Nothing] |
|||
*/ |
|||
object List { |
|||
def add[A](as: List[A], a: A): List[A] = Cons(a, as) |
|||
} |
} |
||
</lang> |
</lang> |