user421104
user421104

Reputation: 43

gensym in Racket

I know gensym can generate symbols, but it seems that there is a global counter there with the base, which can be very large, for example, (define s (gensym 's)) the s can end up with s12345. I am wondering whether there is a way to reset the counter, where the number generated can be small?like s14?

Upvotes: 2

Views: 2447

Answers (1)

Eli Barzilay
Eli Barzilay

Reputation: 29556

There's no way to do that, otherwise it would have been mentioned in the docs. You can only provide a "base" for the new symbol. If you really need a small counter, then it's easy to make up your own gensym using string->unreadable-symbol, for example:

(define gensym
  (let ([counter 0])
    (lambda ([x 'g])
      (if (number? x)
        (set! counter x)
        (begin0 (string->unreadable-symbol
                 (format "~a~a" x counter))
          (set! counter (add1 counter)))))))

Upvotes: 11

Related Questions