comp.lang.ada
 help / color / mirror / Atom feed
From: "Chad  R. Meiners" <chad.rmeiners@gmail.com>
Subject: Re: initialize an array (1-D) at elaboration using an expression based on the index?
Date: Thu, 21 Oct 2010 06:44:47 -0700 (PDT)
Date: 2010-10-21T06:44:47-07:00	[thread overview]
Message-ID: <1f6bad81-e3d2-428b-a1a0-45acc7f96f68@m7g2000yqm.googlegroups.com> (raw)
In-Reply-To: i9amkr$goj$1@speranza.aioe.org

On Oct 15, 7:03 pm, "Nasser M. Abbasi" <n...@12000.org> wrote:
> Ada experts:
>
> I am not able to find an example on this.
>
> I was wondering if in Ada I can initialize some array, where I can fill
> the content of each entry in the array based on some function of the index?
>
> For example, in Fortran one can do this:
> -----------------------
> program main
>    integer, parameter :: N = 10
>    real, dimension(N) :: a=(/(j**2,j=1,N)/)
>
>    print*, a
> end program
> ------------------------
> $ gfortran t3.f90
> $ ./a.exe
>     1.0000000       4.0000000       9.0000000       16.000000 etc....
>
> In Ada, what would I need to do in the initialization below?
> ---------------------------
> procedure t2 is
>     N: constant integer := 10;
>     a: array(1..N) of float := (others=>0.0); --??
> begin
>     null;
> end t2;
> -----------------------------
>
> I know ofcourse I can initialize it in the body by a loop, just wanted
> to see if it is possible to do it in the elaboration.
>
> thanks
> --Nasser

We can borrow a page from either ocaml and F# and set up a generic
initialize function.

I don't have access to a compiler and I haven't programmed in Ada for
years, but below is the general idea.

generic
  type T is array (I) of E;
  with function expression(Index: I) return E;
function init return T is
  val : T;
begin
  for index in I'Range loop
    val(index) := expression(I);
  end loop;
  return val;
end function;

procedure t2 is
  N : constant Integer := 10;

  type Foo is array(1..N) of float;
  function exp(j : Integer) return float is return float(j**2); end
function;
  function initFoo is new init(Foo,exp);

  a : Foo := initFoo;
begin
  null;
end t2;

for those interested how it would be done in F# (which has a type
generic init function for arrays):

let t2 () =
  let N = 10 in
  let a = Array.init N (fun j -> let j = j+1 in j*j |> float)
  in ()



  parent reply	other threads:[~2010-10-21 13:44 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-15 23:03 initialize an array (1-D) at elaboration using an expression based on the index? Nasser M. Abbasi
2010-10-15 23:31 ` Vinzent Hoefler
2010-10-16  0:16   ` Adam Beneschan
2010-10-16  0:29     ` Nasser M. Abbasi
2010-10-16  1:47       ` Robert A Duff
2010-10-16  1:01     ` Randy Brukardt
2010-10-16 10:08     ` Phil Clayton
2010-10-18 15:03       ` Adam Beneschan
2010-10-19  6:29         ` Randy Brukardt
2010-10-20 20:01         ` Phil Clayton
2010-10-19 16:34     ` Britt Snodgrass
2010-10-19 18:05       ` Jeffrey Carter
2010-10-19 19:00         ` Vinzent Hoefler
2010-11-10 14:33     ` Georg Bauhaus
2010-11-10 15:51       ` Adam Beneschan
2010-11-10 17:19         ` Dmitry A. Kazakov
2010-11-10 18:03           ` Adam Beneschan
2010-11-11  1:07         ` Georg Bauhaus
2010-11-11  8:30           ` Dmitry A. Kazakov
2010-11-11 12:02           ` Robert A Duff
2010-11-11 14:19             ` Georg Bauhaus
2010-10-16  0:52 ` Jeffrey Carter
2010-10-16  0:54 ` Gene
2010-10-16  1:11   ` Vinzent Hoefler
2010-10-21 13:44 ` Chad  R. Meiners [this message]
2010-10-24 16:40   ` Shark8
2010-10-24 22:48     ` Phil Clayton
2010-10-25  2:23       ` Shark8
2010-10-29 23:26         ` Phil Clayton
2010-10-31 18:47           ` Shark8
2010-10-31 21:59             ` Georg Bauhaus
2010-11-01  0:45             ` Phil Clayton
2010-11-01  1:55               ` Shark8
2010-10-30  6:34         ` Brian Drummond
2010-10-31 19:00           ` Shark8
2010-10-31 18:09             ` (see below)
2010-10-31 19:35               ` Shark8
2010-10-31 22:47                 ` (see below)
2010-11-01  0:07                   ` Shark8
2010-10-31 23:21                     ` (see below)
2010-10-31 21:26             ` Brian Drummond
2010-11-12 18:10             ` Randy Brukardt
replies disabled

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