From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Re: Implementing Memorize
Date: Wed, 22 Oct 2003 15:00:30 GMT
Date: 2003-10-22T15:00:30+00:00 [thread overview]
Message-ID: <iYwlb.10662$PZ1.812@nwrdny03.gnilink.net> (raw)
In-Reply-To: slrnbpch9n.oa.lutz@taranis.iks-jena.de
"Lutz Donnerhacke" <lutz@iks-jena.de> wrote in message
news:slrnbpch9n.oa.lutz@taranis.iks-jena.de...
> In order to implement a generic memorize, I tried the following
> (see <slrnbpajhj.ns.lutz@taranis.iks-jena.de> for the context)
>
> generic
> with function Callback (n : Natural) return Natural;
> function Action (n : Natural) return Natural;
>
> function Action (n : Natural) return Natural is
> begin
> case n is
> when 0 | 1 => return 1;
> when others => return Callback(n-1) + Callback(n-2);
> end case;
> end Action;
>
> function Fib_Direct is new Action (Fib_Direct); -- won't compile
I can see why this doesn't compile: you are using Fib_Direct to define
Fib_Direct. We could avoid this bit of circularity by declaring another
function to serve as the generic parameter, then implement this other
function as a simple call to the generic instantiation, as follows:
function Fib_Indirect(n : Natural) return Natural;
function Fib_Direct is new Action( Fib_Indirect );
function Fib_Indirect(n : Natural) return Natural is
begin
return Fib_Direct(n);
end Fib_Indirect;
Of course, for the particular problem you posted, I would not use generics
at all. This implementation works quite nicely:
function Fib_Direct(n : Natural) return Natural is
begin
case n is
when 0 | 1 => return 1;
when others => return Fib_Direct(n-1) + Fib_Direct(n-2);
end case;
end Fib_Direct;
next prev parent reply other threads:[~2003-10-22 15:00 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-22 8:52 Implementing Memorize Lutz Donnerhacke
2003-10-22 15:00 ` Frank J. Lhota [this message]
2003-10-22 17:03 ` tmoran
2003-10-23 0:25 ` Georg Bauhaus
-- strict thread matches above, loose matches on Subject: below --
2003-10-22 9:28 christoph.grein
2003-10-22 10:32 ` Lutz Donnerhacke
2003-10-22 10:48 ` Marius Amado Alves
2003-10-22 11:07 ` Lutz Donnerhacke
2003-10-22 11:33 ` Lutz Donnerhacke
2003-10-22 11:56 ` Lutz Donnerhacke
2003-10-22 12:29 ` Marius Amado Alves
2003-10-22 12:52 ` Lutz Donnerhacke
2003-10-22 13:42 ` Marius Amado Alves
2003-10-22 12:08 ` Dmitry A. Kazakov
2003-10-22 12:10 ` Lutz Donnerhacke
2003-10-22 15:23 ` Dmitry A. Kazakov
2003-10-22 19:41 ` Lutz Donnerhacke
2003-10-23 14:36 ` Dmitry A. Kazakov
2003-10-22 19:29 ` Robert I. Eachus
2003-10-22 19:44 ` Lutz Donnerhacke
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox