Skip to content

Instantly share code, notes, and snippets.

@paulp
Created February 16, 2018 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulp/ad8dd1a5cf551afe96c1037add660a52 to your computer and use it in GitHub Desktop.
Save paulp/ad8dd1a5cf551afe96c1037add660a52 to your computer and use it in GitHub Desktop.
object Test {
type List[A] = Option[Cons[A]]
final case class Cons[A](hd: A, tl: Option[Cons[A]]) {
def nth(n: Int): A = if (n == 1) hd else tl.get nth n - 1
}
def list[A](xs: A*): List[A] = if (xs.isEmpty) None else Some(Cons(xs.head, list(xs.tail: _*)))
def single[A](hd: A): Cons[A] = Cons(hd, None)
// (nth (rest (cons 1 nil)) 1)
def lhs = single(1).tl map (_ nth 1)
// (nth nil 1)
def rhs = list[Int]() map (_ nth 1)
def main(argv: Array[String]): Unit = {
// (nth (rest (cons 1 nil)) 1) == (nth nil 1)
assert(lhs == rhs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment