added assignment 3 ex 1.1

This commit is contained in:
Louis Heredero 2025-03-04 16:04:17 +01:00
parent 444c83afb2
commit 53c4036f85
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

View File

@ -14,14 +14,21 @@ class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet() {
if (x < elem) left contains x if (x < elem) left contains x
else if (x > elem) right contains x else if (x > elem) right contains x
else true else true
override def toString = "(" + left + "|" + elem + "|" + right + ")"
} }
class Empty extends IntSet() { object Empty extends IntSet() {
def add(x: Int): IntSet = new NonEmpty(x, new Empty(), new Empty) def add(x: Int): IntSet = new NonEmpty(x, Empty, Empty)
def contains(x: Int): Boolean = false def contains(x: Int): Boolean = false
override def toString = "-"
} }
val t1 = new Empty() val t1 = Empty
val t2 = t1 add 3 val t2 = t1 add 3
val t3 = t1 add 4 add 5 add 2 add 6 val t3 = t1 add 4 add 5 add 2 add 6
t3 contains 4 t3 contains 4
println(Empty) // prints -
println(Empty.add(3)) // prints (-|3|-)
println(Empty.add(3).add(2)) // prints ((-|2|-)|3|-)