Page 21, Exercise 3
ADT Bag is
objects:
a subrange of the integers starting at (INT_MIN) and ending at the
maximum integer (INT_MAX) on the computer
functions:
for
all x, y ∈ Bag; TRUE, FALSE ∈ Boolean and the Boolean operations defined in Problem 4 are available (not, and, or,...)
and where ==, Ø, +, head(s), tail(s) are the usual Bag operations,
where == return TRUE if tw set elements are the same, and TRUE otherwise.
Ø is the empty Bag.
+ adds and element to a Bag.
head(s) extracts the first member in the Bag.
tail(s) extracts a list of all other elements in the Bag. An empty bag contains no tail. A bag with only one element has the emtpy bag has it tail.
| Bag Create(b) | ::= Ø |
| Boolean IsEmpty(b) | ::= if (b ==Ø ) return TRUE return FALSE |
| Boolean IsIn(x, b) | ::= if (IsEmpty(b)) return FALSE if (x == head(b) return TRUE return IsIn(x, Tail(b)) |
| Bag Insert(x,s) | ::= if (IsEmpty(b)) return b return x + b |
| Bag Remove(x, s) | ::= if (IsEmpty(b)) return b if (x == head(b)) return tail(b) return Remove(x, tail(b)) |
end bag