Created
July 25, 2020 12:44
-
-
Save yakkomajuri/47737ce1f33867b3d7114f5824b5a158 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
// If you define a Tree class like this: | |
sealed trait Tree[A] | |
case class Branch[A](l: Tree[A], r: Tree[A], v: A) extends Tree[A] | |
case class Leaf[A](v: A) extends Tree[A] | |
// Then summing up the tree can be done like this: | |
def sum(tree: Tree[Int]): Int = tree match { | |
case Branch(l, r, v) => sum(l) + sum(r) + v | |
case Leaf(v) => v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment