Sunday, April 21, 2013

Lists

A fundamental data structure is the immutable linked list. It is composed of two parts:

  1. Nil: the empty set
  2. Cons: a cell containing an element and the rest of the list
trait IntList ...
class Cons(val head: Int, val tail: IntList) extends IntList ...
class Nil extends IntList
The use of val is syntactic sugar for:
class Cons(_head: Int, _tail: IntList) extends IntList {
      val head = _head
      val tail = _tail
}
This saves the use of _head and _tail. 

No comments:

Post a Comment