Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Created January 31, 2012 06:50
Show Gist options
  • Save jordanlewis/1709288 to your computer and use it in GitHub Desktop.
Save jordanlewis/1709288 to your computer and use it in GitHub Desktop.
PFDS CustomListStack
sealed abstract class LIST[+T]
case object NIL extends LIST[Nothing]
final case class CONS[T](head: T, tail: LIST[T]) extends LIST[T]
object CustomListStack {
def apply[T] = new CustomListStack[T](NIL)
}
class CustomListStack[T] private (private val l: LIST[T]) extends Stack[T] {
def isEmpty = l match {
case NIL => true
case CONS(_, _) => false
}
def cons[U >: T](x: U) = new CustomListStack[U](new CONS[U](x, l))
def head = l match {
case NIL => throw new IllegalArgumentException("Fail")
case CONS(x, _) => x
}
def tail = l match {
case NIL => throw new IllegalArgumentException("Fail")
case CONS(_, x) => new CustomListStack[T](x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment