From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.ada Subject: Re: project euler 26 Date: Sat, 09 Sep 2023 22:04:34 +0100 Organization: A noiseless patient Spider Message-ID: <87sf7nkqhp.fsf@bsb.me.uk> References: <878r9mudvj.fsf@bsb.me.uk> <87a5u1u1yv.fsf@bsb.me.uk> <8734ztttpc.fsf@bsb.me.uk> <87fs3ssl6v.fsf@bsb.me.uk> <87a5u0rts0.fsf@bsb.me.uk> <87jzt3qqlb.fsf@bsb.me.uk> <87o7ieq3ne.fsf@bsb.me.uk> <87tts4ml8z.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: dont-email.me; posting-host="af15565c84ea4ea7db7cc714af21fb35"; logging-data="239319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19pzUmyFfSNSRRYeO7ABK16NFnKMAIMw1c=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Cancel-Lock: sha1:stz/ggmSo5UM3McOSHB5pEGuLcw= sha1:3kprzfZVkADcIDqeT5GvvDhYb9A= X-BSB-Auth: 1.47513d39058ec4707974.20230909220434BST.87sf7nkqhp.fsf@bsb.me.uk Xref: news.eternal-september.org comp.lang.ada:65631 List-Id: "G.B." writes: > On 08.09.23 23:02, Ben Bacarisse wrote: >> "G.B." writes: >> >>> On 07.09.23 01:32, Ben Bacarisse wrote: >>> >>> >>>> A fix (though it's not really ideal) would be to use function >>>> composition here (inventing . as the composition operator): >>> >>> Hm. A stateful, composed function that needs to be applied >>> in a certain way. Is that so different from calling interface >>> subprograms of a certain type? >> There was nothing stateful (as I understand the term) in either function >> being composed. > > The "apparatus" that the computation needs in order to remember > "max so far" looks like part of its state to me. The "max so far" function is not being composed. > Somehow > "the function" needs to operate this state and evaluate it. > Extend this to: > - find the maximum of [the maxima of] these n collections > - find the maximum in this stream at 10 seconds from now. > > Is it possible, or practical, to define a pure function so that > calling it will remember the needed information, n >= 0 > being arbitrary? I am not sure how the discussion got here. Ada is an imperative language so of course one would use "running" state to calculate a maximum. The max_of_F_over_X function just loops like any other loop from First_Of(X) to Last_Of(X) calculating F(This(X)), comparing that with Max_So_Far. These are invented names, of course, because I don't know how it should be done, but the idea is plain imperative code. In fact, it's possible that Ada has some entirely different syntax for this kind of imperative loop. The "fix" to use a composed function was because one proposed solution found the maximum of Element(C) (where C was a cursor) because Element was passed as F, but that of course is not what was wanted. To find the maximum of F I speculated that one could pass F.Element. >>> So, does type composition help? >> My turn to guess now: you are not being serious? I see no connection to >> monads or type composition. > > In the following sense: > There is an object of type So_Far that can remember > objects of any type T, them coming from collections > of type C-of-T. Given the above confusion about what is being composed, I can see the connection with monads. But there is no reason to use the concept of a monad in a language with modifiable state. The maximum should be found as in the posted code with a simple loop. > I'm not sure if the new Ada.Iterator_Interfaces (LRM 5.5.1) > could solve this, also because I really don't know that yet. > But it looks like I'd need instances of specific containers > for instantiation. (That being consistent with Ada's approach > to the STL, I think.) Thanks for the pointer. I am not sure I have time to look in detail, but it looks interesting. -- Ben.