On 1 Sep 2021, at 14:38, Marc Nieper-Wißkirchen <
xxxxxx@nieper-wisskirchen.de> wrote:
> Am Mi., 1. Sept. 2021 um 14:08 Uhr schrieb Daphne Preston-Kendal <
xxxxxx@nonceword.org>:
> […]
>> I found a semantic problem with using fold as the basis of this which seemed to me to be insurmountable. I have since forgotten what it was, though — I’ll take another look and get back to you.
>
> Thanks. I would like to hear about it.
I remembered.
Consider two sequences, one of which is a prefix of the other. Pass both of them to fold, and fold will stop at the end of the shorter sequence (i.e. the one that is a prefix). There’s no way to tell which of them is longer and which should therefore sort later lexicographically.
>> Yes, I too see this as a severe flaw of the SRFI 158 design, but alas I was not around at the time to argue against it. I would have, at the very least, made generators one-argument procedures rather than thunks, and had them return the argument passed when the sequence is finished, instead of the eof-object. Or had them return two values, the extra one being #t if there are more items in the sequence and #f if not.
> <snip>
> Your proposal doesn't remove the imperative nature.
>
> In what sense do you think my proposal is more fiddly?
>
> (define (generator->list gen)
> (let f ((val (gen))
> (if (eof-object? val)
> '()
> (cons val (f (gen)))))
>
> (define (iterator->list it)
> (let f ((it it))
> (it (lambda (it val)
> (cons val (f it)))
> (lambda () '()))))
>
> [Add any convenience syntax if you don't like explicit lambdas.]
>
> I don't see any consing here (except for the explicit "cons"). (And the closures of the two lambdas are constant.)
Okay, now I understand. Yes, you're correct, that would be a good, functional design. But I’m resistent to the idea of having two different iteration protocols in R7RS Large. My proposal could also, hypothetically, be retro-specced into the existing generator design: generators just have to assume the eof-object as the sentinel value if no explicit one is given.
> Marc
Daphne