Re: Eliminate numeric-range over inexact numbers?
Wolfgang Corcoran-Mathe 28 Aug 2020 16:01 UTC
On 2020-08-28 08:30 +0200, Marc Nieper-Wißkirchen wrote:
> Removing inexact numeric ranges would be throwing out the baby with the
> bathwater. (And they are useful, e.g. in conjunction with fold when you
> want to sum some progression.)
Agreed.
> (1) Replace "lower-bound" with "start-index" (which is always exact just
> shifts the indexer). This makes the current constraint unnecessary, so even
> all user-provided indexers will work. This will work smoothly.
This seems like a very good idea. If I understand correctly, though,
from an implementation perspective, we would still need to store a lower
bound. For example,
(define r (numeric-range 5 10))
(range-start-index r) ; => 0
(range-lower-bound r) ; => 5
(The range-lower-bound accessor may not exists, of course.) Then:
(define r* (range-drop r 2))
(range-start-index r*) ; => 2
(range-lower-bound r*) ; => 5
In both cases the indexer is
(lambda (start-index n)
(+ lower-bound (* (+ n start-index) step))).
Thus, we never need to compose indexers, we don't introduce round-off
errors by adjusting lower bounds, and it only costs us the additional
start-index field.
I'd like to examine the SRFI further to see if there are any further
consequences of this change, but it seems like the optimal solution to
me. Many thanks for coming up with this, Marc.
--
Wolfgang Corcoran-Mathe <xxxxxx@sigwinch.xyz>
"Simplicity does not precede complexity, but follows it."
--Alan J. Perlis