comp.lang.ada
 help / color / mirror / Atom feed
From: Mark Lorenzen <mark.lorenzen@ofir.dk>
Subject: Re: ML-like alternatives to out parameters for functions
Date: 18 Mar 2003 19:52:28 +0100
Date: 2003-03-18T19:52:28+01:00	[thread overview]
Message-ID: <m31y14bj83.fsf@valhal.vikingnet> (raw)
In-Reply-To: EkIda.21313$SX.19851@nwrdny03.gnilink.net

"Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net> writes:

...snip...

> 
> This function returns the next random number in the range 0.0 .. 1.0, using
> the state information associated with Gen. I would argue that it would be
> far preferable if the random function could be declared as follows:
> 
>     function Random (Gen : in out Generator) return Uniformly_Distributed;
> 

But again this relies on side-effects on it's parameters. I would go
for a solution like this:

function Random (Gen : in Generator) return Random_Continuation

where

type Random_Continuation is
  record
    Gen   : Generator;
    Value : Uniformly_Distributed;
  end record;

Coming from an ML world, the use of side-effects on subprogram
parameters seems ackward, but I understand that it may be necessary for
performance reasons.

Another example, this time a shortest path algorithm:

function Shortest_Path
  (From_Node : in Node; To_Node : in Node; In_Graph : in Graph)
  return Path;

If From_Node = To_Node, then an empty Path should be returned. But
what if there does not exist a path between From_Node and To_Node?
Should an exception be raised (No, it is not necessarily an
error). Should a non-path value be returned (Yes):

type Optional_Path (Defined : Boolean) is
  record
    case Defined is
      when True  => P : Path;
      when False => null;
    end record;

The style is the purely functional and it avoids the use of an out
mode parameter as validity flag.

So my question is again: Is it the Ada-way to program in a purely
functional style, or is it (for performance reasons) better to use an
imperative style with side-effects on parameters. This is unrelated to
using exceptions in case of errors.

- Mark Lorenzen



  reply	other threads:[~2003-03-18 18:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-17 22:17 ML-like alternatives to out parameters for functions Mark Lorenzen
2003-03-17 21:47 ` Stephen Leake
2003-03-17 23:34   ` Mark Lorenzen
2003-03-18  3:54     ` John R. Strohm
2003-03-18  8:59     ` Preben Randhol
2003-03-18 21:09     ` tmoran
2003-03-18 17:04   ` Frank J. Lhota
2003-03-18 18:52     ` Mark Lorenzen [this message]
2003-03-18 19:16       ` Frank J. Lhota
2003-04-01  3:39         ` Robert I. Eachus
2003-04-01 14:51           ` Frank J. Lhota
2003-03-18  8:37 ` Dmitry A. Kazakov
2003-03-18  9:07 ` Preben Randhol
2003-03-19  7:31 ` Mark Biggar
  -- strict thread matches above, loose matches on Subject: below --
2003-04-02  0:23 Alexandre E. Kopilovitch
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox