From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: project euler 26
Date: Fri, 8 Sep 2023 09:23:13 +0200 [thread overview]
Message-ID: <udei51$3cfd9$1@dont-email.me> (raw)
In-Reply-To: <8734zpo3fz.fsf@bsb.me.uk>
On 2023-09-08 03:32, Ben Bacarisse wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>
>> On 2023-09-07 01:32, Ben Bacarisse wrote:
>>> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>>>
>>>> On 2023-09-06 17:16, Ben Bacarisse wrote:
>>>>
>>> Second, if I try to use a Vector rather than an Ordered_Map, I am told
>>> that:
>>> test2.adb:97:05: error: instantiation error at line 12
>>> test2.adb:97:05: error: no visible subprogram matches the specification for "<"
>>> It would seem that vector cursors can't be compared using < (at least by
>>> default). Maybe the installation needs more arguments.
>>
>> Vector has a proper index type. All you have to do is. Given
>>
>> package Integer_Vectors is
>> new Ada.Containers.Vectors (Integer, Integer);
>>
>> Wrap Element into a function:
>>
>> V : Integer_Vectors.Vector;
>> function Element (Index : Integer) return Integer is
>> begin
>> return V.Element (Index);
>> end Element;
>> ...
>>
>> and use the wrapper.
>
> Sure, but the hope was to write something that does not need new
> code for new situations. That's what makes it reusable.
Why should it be? You wanted to find maximum of a function. Vector is
not a function. It is in mathematical terms, but not in the language
terms. The abstraction for finding maximum in a container is just a
different abstraction.
>> Then you would pass Has_Element for it. For integers you would use wrapped
>> X'Valid (there is no Integer'Valid, unfortunately. Only X'Valid where X is
>> an object).
>
> It's definitely getting what I call cumbersome.
Yes, because you try too hard to make it work where it probably should not.
> I don't think this is incoherent. The Haskell libraries ensure that any
> collection that is logically foldable is indeed foldable.
Ada arrays and library containers do not share interfaces. [It is a long
discussion how they could be]. But similarly, there is no shared
interface between digits of a number in octal base and a container and a
string in UTF-16 encoding. Should there be? No. Should the language
allow adding ad-hoc interfaces to existing types. Yes, and this is
possible in Ada in some very uncomfortable AKA cumbersome way, which is
why "finding maximum" is not a worthy abstraction in Ada.
>>> The bottom line is the last argument should be something very general
>>> like the Period function.
>>> A fix (though it's not really ideal) would be to use function
>>> composition here (inventing . as the composition operator):
>>> Map_Functions.Maximum_At (X.First, X.Last, Period'Access
>>> . Element'Access)
>>> but I don't think Ada has a function composition operator, does it?
>>
>> No as it would require closures.
>
> What closure is required for a function composition? There is no
> environment to "close over".
In Ada a function can use anything visible at its declaration point and
at the location of its body. You can even declare a function inside a
recursively called function and let it see local variables of each
recursive call, in effect having an infinite set of functions.
> That's a lot just to use something that is supposed to be reusable.
[rant on]
An Ada programmer would just write a loop. Abstractions are meant to
abstract the problem domain. If you starting abstract elementary
programming activities, then there might be something wrong with the
language or with you.
Then there is a point about readability. When I see a loop I say, aha
this is what the guy is going to do. When I see a pile of calls of a
dozen generic instances with arbitrary names, I begin to worry.
In my view it is a road to nowhere, for an imperative language at least.
The end of this road can be seen in modern C++. 20 years ago C++ was
severely crippled broken but sometimes enjoyable language. You could
*read* a C++ program. In these days with all libraries it became Forth
on steroids. There is no such thing as a C++ program anymore, just calls
upon calls.
[rant off]
> It only occurred to me after writing the non-generic solution. I
> remember Ada as being something of a pioneer in it's attempt to provide
> generic solutions, so I wondered how far things had come. I don't think
> something really widely reusable is possible in this case.
As I said you think in a wrong direction of abstracting the language
"finding maximum" rather than the problem space, e.g. generalization to
other bases, other mathematical structures etc.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2023-09-08 7:23 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-04 9:19 project euler 26 CSYH (QAQ)
2023-09-04 11:06 ` Niklas Holsti
2023-09-04 12:39 ` Dmitry A. Kazakov
2023-09-04 16:01 ` Ben Bacarisse
2023-09-04 19:20 ` Dmitry A. Kazakov
2023-09-04 20:18 ` Ben Bacarisse
2023-09-04 21:00 ` Dmitry A. Kazakov
2023-09-04 23:16 ` Ben Bacarisse
2023-09-05 7:23 ` Dmitry A. Kazakov
2023-09-05 15:18 ` Ben Bacarisse
2023-09-05 17:08 ` Dmitry A. Kazakov
2023-09-06 1:10 ` Ben Bacarisse
2023-09-06 7:06 ` Dmitry A. Kazakov
2023-09-06 15:16 ` Ben Bacarisse
2023-09-06 15:54 ` Dmitry A. Kazakov
2023-09-06 23:32 ` Ben Bacarisse
2023-09-07 9:02 ` Dmitry A. Kazakov
2023-09-08 1:32 ` Ben Bacarisse
2023-09-08 7:23 ` Dmitry A. Kazakov [this message]
2023-09-09 0:25 ` Ben Bacarisse
2023-09-09 9:32 ` Dmitry A. Kazakov
2023-09-10 1:20 ` Ben Bacarisse
2023-09-10 8:46 ` Dmitry A. Kazakov
2023-09-10 19:22 ` Ben Bacarisse
2023-09-11 6:53 ` Dmitry A. Kazakov
2023-09-11 16:13 ` Ben Bacarisse
2023-09-12 7:17 ` Dmitry A. Kazakov
2023-09-13 12:24 ` Ben Bacarisse
2023-09-14 6:33 ` Dmitry A. Kazakov
2023-09-14 14:30 ` Ben Bacarisse
2023-09-08 6:09 ` G.B.
2023-09-08 21:02 ` Ben Bacarisse
2023-09-09 8:13 ` G.B.
2023-09-09 21:04 ` Ben Bacarisse
2023-09-10 9:11 ` Dmitry A. Kazakov
2023-09-05 17:35 ` moi
2023-09-04 14:23 ` Dmitry A. Kazakov
2023-09-07 7:31 ` Francesc Rocher
2023-09-15 9:07 ` CSYH (QAQ)
2023-09-19 7:59 ` comp.lang.ada
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox