package project1
object sets extends testing.Show {
type Set = Int => Boolean
def contains(s: Set, elem: Int): Boolean = s(elem)
def toString(s: Set): String = {
val xs = for (i <- -1000 to 1000 if contains(s, i)) yield i
xs.mkString("{", ",", "}")
}
def printSet(s: Set) {
println(toString(s))
}
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
}
def main(args : Array[String]) : Unit = {
'contains((x: Int) => x == 1, 1)
}
}