Created
February 16, 2018 09:08
-
-
Save paulp/ad8dd1a5cf551afe96c1037add660a52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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