<< Prev | - Up - | Next >> |
We now illustate the Oz object system by which we can also implement stacks and queues. For missing explanation about Oz object system, we refer to the Oz documentation.
class StackClass from BaseObject
attr stack:nil
meth init stack <- nil end
meth push(E) stack <- E|@stack end
meth pop($)
Stack = @stack
in
stack <- Stack.2
Stack.1
end
meth isEmpty($)
case @stack
of nil then true
else false
end
end
meth toList($) @stack end
end
unit(push :proc{$ X} {Stack push(X)} end
pop :fun{$} {Stack pop($)} end
init :proc{$} {Stack init} end
isEmpty:fun{$} {Stack isEmpty($)} end
toList :fun{$} {Stack toList($)} end)
local
<StackClass>
in
fun{NewStack}
Stack = {New StackClass init}
in
<Stack Object>
end
end
declare
<NewStack>
Stack = {NewStack}
in
{Inspect Stack}
{Inspect {Stack.isEmpty}}
{ForAll [1 2 3 4 5] Stack.push}
{Inspect {Stack.toList}}
<< Prev | - Up - | Next >> |