Re: fxmapping-unfold(-maybe) Wolfgang Corcoran-Mathe 14 Jun 2021 15:44 UTC

On 2021-06-14 17:15 +0200, Marc Nieper-Wißkirchen wrote:
> Am Mo., 14. Juni 2021 um 16:59 Uhr schrieb Wolfgang Corcoran-Mathe <
> xxxxxx@sigwinch.xyz>:
>
> > On 2021-06-14 10:53 -0400, Wolfgang Corcoran-Mathe wrote:
> > > On 2021-06-14 10:23 +0200, Marc Nieper-Wißkirchen wrote:
> > > > To remedy the problem with fxmapping-unfold*, change the semantics of
> > STOP.
> > > > It shall abandon the current continuation and pass the resulting
> > fxmapping
> > > > to the continuation of the call to fxmapping-unfold*. Make
> > INSERT&CONTINUE
> > > > implicit by returning to the continuation of the call to the callback.
> >
> > I just noticed that this is also afflicted by the "tail-loop or die"
> > problem.  The only way to implement these semantics that I can see is
> > to unfold iteratively (sometimes called a "tabulate"), which is
> > usually unidiomatic.
>
> Could you explain to me what you mean by both statements here? It sounds
> very interesting.

As an example, here's the required-tail-call/list version of the
variant unfold:

    (define (accumulate f seed)
      (f (lambda () '())                  ; stop
         (lambda (x seed*)                ; insert & continue
           (cons x (accumulate f seed*)))
         seed))

If we want to rewrite this so that the `stop' procedure returns the
accumulated list, we'd of course have to rewrite this as a tail-loop,
as in your earlier email.

This is not such an enormous difficulty, but it is generally more
convoluted to construct inductively-defined structures iteratively.
In the list example, it's simply a matter of reversing the result, but
constructing trees this way, e.g., can be a pain.  It's natural to
construct inductively-defined structures recursively; as Olin's
comment to SRFI 1 says, "Don't stand on your head to iterate!".

In some cases, recursive implementations might also be more
efficient.  Building a radix tree (see the SRFI 224 sample
implementation) recursively accumulates call frames, of course, but
building one tail-recursively accumulates thunks.  Depending on the
Scheme implementation, the latter may be (and often are) more
expensive.

--
Wolfgang Corcoran-Mathe  <xxxxxx@sigwinch.xyz>

"I think, to most people, scripting is a lot like obscenity.  I can't
define it, but I'll know it when I see it." --Larry Wall