From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,d00514eb0749375b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!i21g2000yqg.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Re: initialize an array (1-D) at elaboration using an expression based on the index? Date: Fri, 15 Oct 2010 17:54:05 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 184.12.87.218 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1287190445 20817 127.0.0.1 (16 Oct 2010 00:54:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 16 Oct 2010 00:54:05 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: i21g2000yqg.googlegroups.com; posting-host=184.12.87.218; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; HPNTDF; .NET4.0C),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:15526 Date: 2010-10-15T17:54:05-07:00 List-Id: On Oct 15, 7:03=A0pm, "Nasser M. Abbasi" 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 inde= x? > > For example, in Fortran one can do this: > ----------------------- > program main > =A0 =A0integer, parameter :: N =3D 10 > =A0 =A0real, dimension(N) :: a=3D(/(j**2,j=3D1,N)/) > > =A0 =A0print*, a > end program > ------------------------ > $ gfortran t3.f90 > $ ./a.exe > =A0 =A0 1.0000000 =A0 =A0 =A0 4.0000000 =A0 =A0 =A0 9.0000000 =A0 =A0 =A0= 16.000000 etc.... > > In Ada, what would I need to do in the initialization below? > --------------------------- > procedure t2 is > =A0 =A0 N: constant integer :=3D 10; > =A0 =A0 a: array(1..N) of float :=3D (others=3D>0.0); --?? > begin > =A0 =A0 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. It's ugly, but you can get the effect with something like: Initializer_Index : Natural :=3D 0; function Initial_Function_Of_Index return Float is begin Initializer_Index :=3D Initializer_Index + 1; return 2.0 * Float(Initializer_Index); end; A : array(1 .. 100) of Float :=3D (others =3D> Initializer_Function_Of_Index); What you're asking about is called a comprehension or list comprehension. I agree it would be nice to have them.