Anonymous user
Power set: Difference between revisions
→{{header|Ada}} split solution in modules, to be used by other tasks
(Nimrod -> Nim) |
|||
Line 23:
=={{header|Ada}}==
We start with specifying a generic package Power_Set, which holds a set of positive integers. Actually, it holds a multiset, i.e., integers are allowed to occur more than once.
This solution prints the power set of words read from the command line.▼
<lang ada>with Ada.Text_IO, Ada.Command_Line;▼
▲procedure Power_Set is
type
generic
with procedure Visit(S: Set);
First: Boolean := True;▼
end Power_Set;</lang>
begin ▼
Ada.Text_IO.Put("{ ");▼
The implementation of Power_Set is as follows:
for Item of Items loop▼
if First then ▼
<lang Ada>package body Power_Set is
First := False; -- no comma needed▼
procedure All_Subsets(S: Set) is
Ada.Text_IO.Put(", "); -- comma, to separate the items▼
end if;▼
Ada.Text_IO.Put(Ada.Command_Line.Argument(Item));▼
end loop;▼
Ada.Text_IO.Put_Line(" }");▼
end Print_Set;▼
Tail: Set := Unmarked(Unmarked'First+1 .. Unmarked'Last);
if Unmarked = Empty_Set then
Visit(Marked);
Visit_Sets(Tail, Marked & Unmarked(Unmarked'First));
Visit_Sets(Tail, Marked);
end Visit_Sets;
begin
Visit_Sets(S, Empty_Set);
end All_Subsets;
▲ else
end if;▼
end Power_Set;</lang>
Set: List(1 .. Ada.Command_Line.Argument_Count);▼
▲<lang ada>with Ada.Text_IO, Ada.Command_Line, Power_Set;
procedure Print_Power_Set is
procedure Print_Set(Items: Power_Set.Set) is
begin
else
procedure Print_All_Subsets is new Power_Set.All_Subsets(Print_Set);
begin
for I in Set'Range loop -- initialize set
Line 65 ⟶ 88:
end loop;
Print_All_Subsets(Set); -- do the work
end
{{out}}
|