| // Church-encoded cons list | |
| trait MyList[A] { | |
| def foldRight[B]: (A => B => B) => B => B | |
| } | |
| object MyList { | |
| def nil[A]: MyList[A] = new MyList[A] { | |
| def foldRight[B] = | |
| _ => z => z | |
| } | |
| def cons[A](h: A)(t: MyList[A]): MyList[A] = | |
| new MyList[A] { | |
| def foldRight[A] = | |
| f => z => | |
| f(h)(t.foldRight(f)(z)) | |
| } | |
| // Mutual inverses | |
| def -->[A](a: List[A]): MyList[A] = | |
| a.foldRight(nil[A])((a, b) => cons[A](a)(b)) | |
| def <--[A](a: MyList[A]): List[A] = | |
| a.foldRight[List[A]](a => b => a :: b)(Nil) | |
| // Mutual inverses | |
| // scala.List[A]=MyList[A] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment