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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ed1e3e7e7d67edd4 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!attbi_s21.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Newbie's question References: <1202740198.391371@athprx03> In-Reply-To: <1202740198.391371@athprx03> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 12.201.97.213 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s21 1202751767 12.201.97.213 (Mon, 11 Feb 2008 17:42:47 GMT) NNTP-Posting-Date: Mon, 11 Feb 2008 17:42:47 GMT Organization: AT&T ASP.att.net Date: Mon, 11 Feb 2008 17:42:47 GMT Xref: g2news1.google.com comp.lang.ada:19759 Date: 2008-02-11T17:42:47+00:00 List-Id: Christos Chryssochoidis wrote: Apparently you found a compiler error. Some comments on your code: > Result : access Int_Array; > begin > for I in Elements'Range loop > if Predicate(Elements(I)) then > Tmp_List.Append(Elements(I)); > end if; > end loop; > > Result := new Int_Array(1..Index(Tmp_List.Length)); This is OK for a little test program like this, but in real code, you have a memory leak from this use of access values and heap allocation that you should want to avoid. You can do that by moving the declaration of Result into Copy_List below: Result : Int_Array (1 .. Index (Tmp_List.Length) ); You might also find an unbounded array (Ada.Containers.Vectors) more suited than a list here, since you can use the same index for both. > Copy_List: > declare > Tmp_List_Cursor : Int_Lists.Cursor := Tmp_List.First; > I : Integer := 1; > begin > while Int_Lists.Has_Element(Tmp_List_Cursor) loop > Result(I) := Int_Lists.Element(Tmp_List_Cursor); > Tmp_List_Cursor := Int_Lists.Next(Tmp_List_Cursor); > I := I + 1; > end loop; > end Copy_List; Rather than using a cursor and a loop, I would probably use the Iterate procedure to convert the list into an array. You could also make Filter recursive and eliminate Result and Tmp_List altogether: begin -- Filter if Elements'Length <= 0 then return Elements; elsif Predicate (Elements (Elements'First) ) then return Elements'First & Filter (Elements (Elements'First + 1 .. Elements'Last), Predicate); else return Filter (Elements (Elements'First + 1 .. Elements'Last), Predicate); end if; end Filter; > function Greater_Equal_3(Element :Integer) return Boolean is > begin > if Element >= 3 then > return True; > else > return False; > end if; > end Greater_Equal_3; This should be return Element >= 3; Personally, I'd make filter generic rather than passing the Predicate function as a subprogram parameter. -- Jeff Carter "Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!" Monty Python's Flying Circus 53