Skip to content

Instantly share code, notes, and snippets.

@nelsonblaha
Created April 16, 2013 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonblaha/5400086 to your computer and use it in GitHub Desktop.
Save nelsonblaha/5400086 to your computer and use it in GitHub Desktop.
Problem with Coursera Scala video 3.1 code
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