Created
October 8, 2012 21:26
-
-
Save sumerman/3855089 to your computer and use it in GitHub Desktop.
Scala class hierarchies vs Haskell algebraic-style vs Scala match
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
data IntSet = Empty | |
| Node { val :: Int, | |
left :: IntSet, | |
right :: IntSet } deriving Show | |
incl Empty x = Node x Empty Empty | |
incl node x | x < val node = node { left = incl (left node) x } | |
| x > val node = node { right = incl (right node) x } | |
| otherwise = node | |
contains Empty _ = False | |
contains node x | val node == x = True | |
| val node > x = contains (left node) x | |
| val node < x = contains (right node) x | |
empty = Empty | |
set <-- elem = incl set elem | |
s1 = empty <-- 1 <-- 3 <-- 0 | |
c2 = s1 `contains` 2 | |
c3 = s1 `contains` 3 |
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 IntSets { | |
val s = Empty incl 1 incl 3 incl 0 | |
s contains 2 | |
s contains 3 | |
} | |
abstract class IntSet { | |
def incl(x: Int): IntSet | |
def contains(x: Int): Boolean | |
} | |
object Empty extends IntSet { | |
def contains(x: Int): Boolean = false | |
def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty) | |
override def toString = "." | |
} | |
class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet { | |
def contains(x: Int): Boolean = | |
if (x < elem) left contains x | |
else if (x > elem) right contains x | |
else true | |
def incl(x: Int): IntSet = | |
if (x < elem) new NonEmpty(elem, left incl x, right) | |
else if (x > elem) new NonEmpty(elem, left, right incl x) | |
else this | |
override def toString = "{" + left.toString + "," + elem + "," + right.toString + "}" | |
} |
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 IntSets { | |
val s = Empty incl 1 incl 3 incl 0 | |
s contains 2 | |
s contains 3 | |
} | |
abstract class IntSet { | |
def incl(x: Int): IntSet = this match { | |
case Empty => new NonEmpty(x, Empty, Empty); | |
case NonEmpty(e: Int, l: IntSet, r: IntSet) => | |
if (x < e) new NonEmpty(e, l incl x, r) | |
else if (x > e) new NonEmpty(e, l, r incl x) | |
else this | |
} | |
def contains(x: Int): Boolean = this match { | |
case Empty => false; | |
case NonEmpty(e: Int, l: IntSet, r: IntSet) => | |
if (x < e) l contains x | |
else if (x > e) r contains x | |
else true | |
} | |
} | |
case object Empty extends IntSet { | |
override def toString = "." | |
} | |
case class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet { | |
override def toString = "{" + left.toString + "," + elem + "," + right.toString + "}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment