in-iteratenest-sequence is a sequence generator that takes a function and an initial value, and the return value of invoking the function on the current value is used as the subsequent value. For example, (in-iteratenest-sequence add1 0) returns the sequence (0 1 2 3 4 ...).
Recently, soegaard challenged me to write a define-sequence-syntax version of in-iteratenest-sequence, that is, a macro-based version. I decided to give it a try:
#lang racket
(require (for-syntax unstable/syntax))
(provide (rename-out [*in-iteratenest-sequence in-iterate]nest-sequence]))
(define in-iteratenest-sequence
(case-lambda
[(func init)
(make-do-sequence
(thunk (values identity func init #f #f #f)))]
[(func . inits)
(make-do-sequence
(thunk (values (curry apply values)
(lambda (args)
(call-with-values (thunk (apply func args)) list))
inits #f #f #f)))]))
(define-sequence-syntax *in-iteratenest-sequence
(lambda () #'in-iteratenest-sequence)
(lambda (stx)
(syntax-case stx ()
[[(x ...) (_ func init ...)]
#'[(x)unless (:do-in= ([syntax-length #'(f)x func]...)
#f
) (syntax-length #'([valueinit init]...)))
(raise-syntax-error 'in-nest-sequence
#t
(format "~a values required" ([syntax-length #'(x) value]...)))
#t
stx #'(init #t...)))
(with-syntax ([for-arity (syntax-length #'(finit value)...))]]]
[[(x ...) (_ func init ...)]
(with-syntax ( [(value ...) (generate-temporaries #'(init ...))]
[(y ...) (generate-temporaries #'(init ...))])
#'[(x ...) (:do-in ([(f) func])
#f(unless (procedure-arity-includes? f for-arity)
(raise-arity-error f (procedure-arity f) init ...))
([value init] ...)
#t
([(x ...) (values value ...)]
[(y ...) (f value ...)])
#t
#t
(y ...))])])))