Skip to content

Instantly share code, notes, and snippets.

@oluies
Created February 17, 2011 20:19
Show Gist options
  • Save oluies/832587 to your computer and use it in GitHub Desktop.
Save oluies/832587 to your computer and use it in GitHub Desktop.
stree
sealed abstract class Tree[A]
case class Leaf[A](elem : A) extends Tree[A]
case class Node[A](left : Tree[A], right : Tree[A]) extends Tree[A]
val t = Node(Leaf(11), Node(Leaf(13),Leaf(15) ))
def sum(t: Tree[Int]):Int = t match {
case Leaf(i) => i
case Node(l,r) => sum(l) + sum(r)
case _ => error("FUBAR tree")
}
println(sum(t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment