In JavaScript one may create Generator functions by using the function*
syntax, as described here: Iterators and generators - JavaScript | MDN
Is it possible to do the same in Racket?
What I would like to obtain is, based on a given list of symbols, a generator that produces new lists by inserting e.g. the symbols +
and *
at varying places in the given list.
You don't need to declare the function differently. You can just use generators in Racket; the docs are here: 4.15.3 Generators
1 Like
I think this may be the way to write a function seq-gen
that produces a generator, in this case a generator that generates the elements given in the seq
list:
(require racket/generator)
(define (seq-gen seq)
(generator ()
(let loop ([x seq])
(if (null? x)
0
(begin
(yield (car x))
(loop (cdr x)))))))
(let ([sgen (seq-gen '(A B C))])
(list (sgen) (sgen) (sgen) (sgen)))
The reason I had problem with this in the beginning was that I forgot to specify (require racket/generator)
.