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: 103376,d00514eb0749375b X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!m7g2000yqm.googlegroups.com!not-for-mail From: "Chad R. Meiners" Newsgroups: comp.lang.ada 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) Organization: http://groups.google.com Message-ID: <1f6bad81-e3d2-428b-a1a0-45acc7f96f68@m7g2000yqm.googlegroups.com> References: NNTP-Posting-Host: 129.55.200.20 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1287668687 6863 127.0.0.1 (21 Oct 2010 13:44:47 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 21 Oct 2010 13:44:47 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: m7g2000yqm.googlegroups.com; posting-host=129.55.200.20; posting-account=XRGbKgoAAACag8f1Ww4XGf81DDZtyfbX User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729; .NET4.0E),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:15626 Date: 2010-10-21T06:44:47-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. > > 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) :=3D expression(I); end loop; return val; end function; procedure t2 is N : constant Integer :=3D 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 :=3D 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 () =3D let N =3D 10 in let a =3D Array.init N (fun j -> let j =3D j+1 in j*j |> float) in ()