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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4180a73b05d119c7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-03-18 09:46:05 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news.algonet.se!algonet!newsfeed1.uni2.dk!news.get2net.dk.POSTED!53ab2750!not-for-mail Sender: malo@valhal.vikingnet Newsgroups: comp.lang.ada Subject: Re: ML-like alternatives to out parameters for functions References: From: Mark Lorenzen Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: 18 Mar 2003 19:52:28 +0100 NNTP-Posting-Host: 62.84.221.216 X-Complaints-To: abuse@colt-telecom.dk X-Trace: news.get2net.dk 1048009564 62.84.221.216 (Tue, 18 Mar 2003 18:46:04 CET) NNTP-Posting-Date: Tue, 18 Mar 2003 18:46:04 CET Organization: Colt Telecom Kunde Xref: archiver1.google.com comp.lang.ada:35469 Date: 2003-03-18T19:52:28+01:00 List-Id: "Frank J. Lhota" 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