• How can I make Reduce run from right to =?UTF-8?B?bGVmdD8=?=

    From b.mcguinness747@b.mcguinness747@gmail.com (Brian9000) to comp.lang.ada on Tue Dec 10 17:20:53 2024
    From Newsgroup: comp.lang.ada

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From J-P. Rosen@rosen@adalog.fr to comp.lang.ada on Tue Dec 10 18:54:27 2024
    From Newsgroup: comp.lang.ada

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From J-P. Rosen@rosen@adalog.fr to comp.lang.ada on Tue Dec 10 18:57:45 2024
    From Newsgroup: comp.lang.ada

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Jeffrey R.Carter@spam.jrcarter.not@spam.acm.org.not to comp.lang.ada on Tue Dec 10 22:04:02 2024
    From Newsgroup: comp.lang.ada

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From b.mcguinness747@b.mcguinness747@gmail.com (Brian9000) to comp.lang.ada on Wed Dec 11 00:15:57 2024
    From Newsgroup: comp.lang.ada

    Ok. Thanks for your help.

    --- Brian
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From b.mcguinness747@b.mcguinness747@gmail.com (Brian9000) to comp.lang.ada on Wed Dec 11 01:38:59 2024
    From Newsgroup: comp.lang.ada

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114