From: Christos Chryssochoidis <C.Chryssochoidis@gmail.com>
Subject: Re: Newbie's question [SOLVED]
Date: Mon, 11 Feb 2008 18:28:10 +0200
Date: 2008-02-11T18:28:10+02:00 [thread overview]
Message-ID: <1202747290.968709@athprx04> (raw)
In-Reply-To: <1202740198.391371@athprx03>
I changed version of GNAT and now the code runs ok.
Christos Chryssochoidis wrote:
>
> Hello,
>
> I 'm experimenting with Ada, and I'm having trouble getting some toy
> code running. The code is the following: (hope it isn't too complicated)
>
>
> -- This is a program to test passing subprograms as arguments to other
> subprograms.
> -- It consists of a main program, "Test_Filter", and two nested
> subprograms: "Filter" and "Greater_Equal_3". The "Filter" subprogram
> takes as arguments an array of Integers and a function (predicate), and
> returns an array of the same type, having only those elements of the
> input array that satisfy the given predicate. For some reason that I
> can't figure out, I'm getting a "segmentation fault" when I run the
> executable. (It compiles fine.)
>
>
> with Ada.Containers.Doubly_Linked_Lists;
> with Ada.Text_IO;
>
> procedure Test_Filter is
>
> subtype Index is Natural;
> type Int_Array is array(Index range<>) of Integer;
>
>
> function Filter(Elements : Int_Array; Predicate : not null
> access function(Element : Integer) return
> Boolean) return Int_Array is
> package Int_Lists is new
> Ada.Containers.Doubly_Linked_Lists(Element_Type => Integer);
> Tmp_List : Int_Lists.List;
> 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));
> 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;
>
> return Result.all;
>
> 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;
>
>
> Int_Array1 : Int_Array := Int_Array'(1,2,3,4,5);
> Int_Array2 : Int_Array(1..3);
>
> begin
> Int_Array2 := Filter(Int_Array1, Greater_Equal_3'Access);
> for I in Int_Array1'Range loop
> Ada.Text_IO.Put_Line(Int_Array1(I)'Img);
> end loop;
>
> Ada.Text_IO.Put_Line("");
>
> for I in Int_Array2'Range loop
> Ada.Text_IO.Put_Line(Int_Array2(I)'Img);
> end loop;
>
> end Test_Filter;
>
>
> Thanks very much for any help.
next prev parent reply other threads:[~2008-02-11 16:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-11 14:31 Newbie's question Christos Chryssochoidis
2008-02-11 16:28 ` Christos Chryssochoidis [this message]
2008-02-11 17:42 ` Jeffrey R. Carter
2008-02-12 22:56 ` Christos Chryssochoidis
2008-02-13 11:06 ` Christos Chryssochoidis
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox