Quote:Original post by MDI
The horrific C++ templated mess gets transformed to a few lines of clean O'Caml. Functional languages really are king.
I'd like to remind you that equivalent solution in O'Caml hasn't arrived yet. And it won't, until O'Caml supports GADTs.
Since Scala added support for GADTs recently, I can give you a glimpse of what's to be expected:
object wtf{ abstract class Term[T] case class Lit(i : Int) extends Term[Int] case class Inc(t : Term[Int]) extends Term[Int] case class IsZ(t : Term[Int]) extends Term[boolean] case class If[T](c : Term[boolean], t : Term[T], e : Term[T]) extends Term[T] case class Pai[A, B](c : Term[A], t : Term) extends Term[Pair[A, B]] case class Fst[A, B](t : Term[Pair[A, B]]) extends Term[A] case class Snd[A, B](t : Term[Pair[A, B]]) extends Term def eval[T](t0 : Term[T]) : T = { t0 match { case Lit(i) => i case Inc(t) => eval(t) + 1 case IsZ(t) => eval(t) == 0 case If(b, t, e) => if (eval(b)) eval(t) else eval(e) case Pai(t, u) => Pair(eval(t), eval(u)) case Fst(t) => eval(t) match { case Pair(x, _) => x } case Snd(t) => eval(t) match { case Pair(_, x) => x } } } def main(args : Array[String]) : Unit = { val t = Pai(IsZ(Lit(0)), If(IsZ(Inc(Lit(10))), Lit(1337), Lit(999))) Console.println(eval(t).toString()) }}