From 53c4036f856bf9d589608e026f0ce1a3d56723c7 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 4 Mar 2025 16:04:17 +0100 Subject: [PATCH] added assignment 3 ex 1.1 --- src/Assignment3/IntSet.sc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Assignment3/IntSet.sc b/src/Assignment3/IntSet.sc index a469e0b..38e9a65 100644 --- a/src/Assignment3/IntSet.sc +++ b/src/Assignment3/IntSet.sc @@ -14,14 +14,21 @@ class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet() { if (x < elem) left contains x else if (x > elem) right contains x else true + + override def toString = "(" + left + "|" + elem + "|" + right + ")" } -class Empty extends IntSet() { - def add(x: Int): IntSet = new NonEmpty(x, new Empty(), new Empty) +object Empty extends IntSet() { + def add(x: Int): IntSet = new NonEmpty(x, Empty, Empty) def contains(x: Int): Boolean = false + override def toString = "-" } -val t1 = new Empty() +val t1 = Empty val t2 = t1 add 3 val t3 = t1 add 4 add 5 add 2 add 6 -t3 contains 4 \ No newline at end of file +t3 contains 4 + +println(Empty) // prints - +println(Empty.add(3)) // prints (-|3|-) +println(Empty.add(3).add(2)) // prints ((-|2|-)|3|-) \ No newline at end of file