comp.lang.ada
 help / color / mirror / Atom feed
From: b.mcguinness747@gmail.com (Brian9000)
Subject: Re: How can I make Reduce run from right to left?
Date: Wed, 11 Dec 2024 01:38:59 +0000	[thread overview]
Message-ID: <57349e0bb1125f359b96c743a706a087@www.novabbs.com> (raw)
In-Reply-To: aa2e9cf6fcb092ba4e227a41b05ae707@www.novabbs.com

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

      parent reply	other threads:[~2024-12-11  1:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]
replies disabled

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