# Generator functions?

**URL:** <https://racket.discourse.group/t/generator-functions/1020>\
**Category:** Questions & Answers\
**Created:** [May 23, 2022, 3:57pm UTC](https://racket.discourse.group/t/generator-functions/1020 "2022-05-23T15:57:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkleiser](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jkleiser/32/498_2.png) [@jkleiser](https://racket.discourse.group/u/jkleiser)\
**Post date:** [May 23, 2022, 3:57pm UTC](https://racket.discourse.group/t/generator-functions/1020/1 "2022-05-23T15:57:09Z")

</div>

In JavaScript one may create Generator functions by using the `function*` syntax, as described here: [Iterators and generators - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)  
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.

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [May 23, 2022, 4:43pm UTC](https://racket.discourse.group/t/generator-functions/1020/2 "2022-05-23T16:43:33Z")

</div>

You don't need to declare the function differently. You can just use generators in Racket; the docs are here: [4.15.3&nbsp;Generators](https://docs.racket-lang.org/reference/Generators.html)

---

<div class="post-metadata">

**Author:** ![jkleiser](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jkleiser/32/498_2.png) [@jkleiser](https://racket.discourse.group/u/jkleiser)\
**Post date:** [May 23, 2022, 8:51pm UTC](https://racket.discourse.group/t/generator-functions/1020/3 "2022-05-23T20:51:41Z")

</div>

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:

```scheme
(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)`.
