char-generator Bradley Lucier 14 Jan 2024 17:44 UTC

The draft document states:

char-generator does not use an SRFI 14 char-set. This means, for
implementations that use Unicode, char-generator may generate characters
that are not valid Unicode code points. I think this is the correct
behavior, since invalid code points fulfill the char? predicate.

First question: Why do "invalid code points fulfill the char?
predicate"?  It does seem to be true, at least for Gambit, but is there
some document somewhere that specifies this?

Second question: The character generator in SRFI 231's test-arrays.scm,
which was written under Gambit, is

(define (random-inclusive a #!optional b)
   (if b
       (+ a (test-random-integer (- b a -1)))
       (test-random-integer (+ a 1))))

(define (random-char)
   (let ((n (random-inclusive (##max-char-code))))
     (if (or (fx< n #xd800)
             (fx< #xdfff n))
         (integer->char n)
         (random-char))))

This is because (integer->char n) throws an error if n is not a valid
Unicode code point.
=============================
Aside: In Gambit, while (##integer->char n) does no error checking, it
also does things like:

 > (##integer->char -1)
#f
=============================
So I don't believe that the sample implementation

     (define (char-generator)
       (gcons* #\null
               (gmap integer->char (make-random-integer-generator 0
max-char))))

will work on Gambit.

I guess I would propose that (char-generator) return only valid
characters according to whatever the implementation supports.

Brad