Power set: Difference between revisions

(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<lang Ada>package Power_Set is
procedure Power_Set is
type ListSet is array (Positive range <>) of Positive;
EmptyEmpty_Set: ListSet(1 .. 0);
generic
procedure Print_All_Subsets(Set: List; Printable: List:= Empty) is
with procedure Visit(S: Set);
procedure Print_SetAll_Subsets(ItemsS: ListSet); -- calles Visit once for each subset isof S
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
else
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: Listprocedure Visit_Sets(Unmarked:= Set(Set'First+1; ..Marked: Set'Last); is
Tail: Set := Unmarked(Unmarked'First+1 .. Unmarked'Last);
begin
if Unmarked = Empty_Set then
Visit(Marked);
else
Visit_Sets(Tail, Marked & Unmarked(Unmarked'First));
Visit_Sets(Tail, Marked);
end if;
end Visit_Sets;
begin
Visit_Sets(S, Empty_Set);
if Set = Empty then
end All_Subsets;
Print_Set(Printable);
else
Print_All_Subsets(Tail, Printable & Set(Set'First));
Print_All_Subsets(Tail, Printable);
end if;
end Print_All_Subsets;
end Power_Set;</lang>
Set: List(1 .. Ada.Command_Line.Argument_Count);
 
ThisThe solutionmain program prints the power set of words read from the command line.
 
<lang ada>with Ada.Text_IO, Ada.Command_Line, Power_Set;
 
procedure Print_Power_Set is
procedure Print_Set(Items: Power_Set.Set) is
First: Boolean := True;
begin
Ada.Text_IO.Put("{ ");
for Item of Items loop
if First then
First := False; -- no comma needed
else
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;
procedure Print_All_Subsets is new Power_Set.All_Subsets(Print_Set);
Set: ListPower_Set.Set(1 .. Ada.Command_Line.Argument_Count);
begin
for I in Set'Range loop -- initialize set
Line 65 ⟶ 88:
end loop;
Print_All_Subsets(Set); -- do the work
end Power_Set;</lang>
 
{{out}}
Anonymous user