comp.lang.ada
 help / color / mirror / Atom feed
* How can I make Reduce run from right to left?
@ 2024-12-10 17:20 Brian9000
  2024-12-10 17:54 ` J-P. Rosen
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Brian9000 @ 2024-12-10 17:20 UTC (permalink / raw)


I am trying to find a way to make Reduce operate from right to left like
APL reduction, but this doesn't seem to work.

I wrote a test program:

pragma Ada_2022;
pragma Extensions_Allowed (On);

with Ada.Text_IO;

procedure Reverse_Reduction is
  type Real   is digits 15;
  type Vector is array(Natural range <>) of Real;

  data : constant Vector := [for i in 0 .. 11 => Real (i + 1)];
begin
  for i in data'Range loop
    Ada.Text_IO.Put_Line (i'Image & ": " & data(i)'Image);
  end loop;

  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put_Line (Real'Image ([ for i in 1 .. data'Last => data(i)
]'Reduce("-", data(0))));
  Ada.Text_IO.Put_Line (Real'Image ([ for i in reverse 0 .. data'Last -
1 => data(i) ]'Reduce("-", data(data'Last))));
end Reverse_Reduction;

but the compiler won't accept "reverse" in the next-to-last line, even
though you can use it in a normal loop.  The other lines compile and run
without any problems.

$ gnatmake reverse_reduction.adb
x86_64-linux-gnu-gcc-12 -c reverse_reduction.adb
reverse_reduction.adb:18:47: error: missing operand
x86_64-linux-gnu-gnatmake-12: "reverse_reduction.adb" compilation error

Of course I can just write a normal loop to do this, but I wanted to see
if I could do it with Reduce.

--- Brian

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-12-11  1:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-10 17:20 How can I make Reduce run from right to left? Brian9000
2024-12-10 17:54 ` J-P. Rosen
2024-12-10 17:57 ` J-P. Rosen
2024-12-10 21:04 ` Jeffrey R.Carter
2024-12-11  0:15 ` Brian9000
2024-12-11  1:38 ` Brian9000

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox