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

* Re: How can I make Reduce run from right to left?
  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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: J-P. Rosen @ 2024-12-10 17:54 UTC (permalink / raw)


Le 10/12/2024 à 18:20, Brian9000 a écrit :
> 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.
ARM 2022/4.5.10(6/5):
The iterated_element_association of a value_sequence shall not have a 
key_expression, nor shall it have a loop_parameter_specification that 
has the reserved word reverse.

-- 
AEiC 2025 logo <https://www.ada-europe.org/conference2025>
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
https://www.adalog.fr https://www.adacontrol.fr

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

* Re: How can I make Reduce run from right to left?
  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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: J-P. Rosen @ 2024-12-10 17:57 UTC (permalink / raw)


Le 10/12/2024 à 18:20, Brian9000 a écrit :
 > 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.ARM 2022/4.5.10(6/5):
The iterated_element_association of a value_sequence shall not have a 
key_expression, nor shall it have a loop_parameter_specification that 
has the reserved word reverse.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
https://www.adalog.fr https://www.adacontrol.fr

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

* Re: How can I make Reduce run from right to left?
  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
  4 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R.Carter @ 2024-12-10 21:04 UTC (permalink / raw)


On 2024-12-10 18:20, Brian9000 wrote:
>   Ada.Text_IO.Put_Line (Real'Image ([ for i in reverse 0 .. data'Last -
> 1 => data(i) ]'Reduce("-", data(data'Last))));

Presumably you could do

[for I in 0 .. Data'Last - 1 => Data (Data'Last - 1 - I)]'Reduce

Or write a Reverse function

Reverse (Data (0 .. Data'Last - 1) )'Reduce

-- 
Jeff Carter
"An essential part of teaching Ada is not
the technical details, but the message of
software engineering."
Jean-Pierre Rosen
167

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

* Re: How can I make Reduce run from right to left?
  2024-12-10 17:20 How can I make Reduce run from right to left? Brian9000
                   ` (2 preceding siblings ...)
  2024-12-10 21:04 ` Jeffrey R.Carter
@ 2024-12-11  0:15 ` Brian9000
  2024-12-11  1:38 ` Brian9000
  4 siblings, 0 replies; 6+ messages in thread
From: Brian9000 @ 2024-12-11  0:15 UTC (permalink / raw)


Ok.   Thanks for your help.

--- Brian

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

* Re: How can I make Reduce run from right to left?
  2024-12-10 17:20 How can I make Reduce run from right to left? Brian9000
                   ` (3 preceding siblings ...)
  2024-12-11  0:15 ` Brian9000
@ 2024-12-11  1:38 ` Brian9000
  4 siblings, 0 replies; 6+ messages in thread
From: Brian9000 @ 2024-12-11  1:38 UTC (permalink / raw)


I finally got an APL-like reduction using Ada Reduce:

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)];

  function Minus (Accumulator : Real; Value : Real) return Real is
(Value - Accumulator);
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 0 .. data'Last - 1 =>
data(data'Last - 1 - i) ]'Reduce(Minus, data(data'Last))));
end Reverse_Reduction;


$ ./reverse_reduction
 0:  1.00000000000000E+00
 1:  2.00000000000000E+00
 2:  3.00000000000000E+00
 3:  4.00000000000000E+00
 4:  5.00000000000000E+00
 5:  6.00000000000000E+00
 6:  7.00000000000000E+00
 7:  8.00000000000000E+00
 8:  9.00000000000000E+00
 9:  1.00000000000000E+01
 10:  1.10000000000000E+01
 11:  1.20000000000000E+01

-6.00000000000000E+00


In Gnu APL:

      ⍳12
1 2 3 4 5 6 7 8 9 10 11 12
      -/⍳12
¯6

--- 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