Created
April 16, 2013 22:11
-
-
Save nelsonblaha/5400086 to your computer and use it in GitHub Desktop.
Problem with Coursera Scala video 3.1 code
This file contains hidden or 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
package objsets | |
abstract class IntSet { | |
def incl(x:Int): IntSet | |
def contains(x:Int): Boolean | |
} | |
class Empty extends IntSet { | |
def contains(x: Int): Boolean = false | |
def incl(x: Int): IntSet = new NonEmpty(x, new Empty, new Empty) | |
} | |
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 + elem + right + "}" | |
} | |
object Video1 { | |
val empty = new Empty | |
empty.contains(2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment