[racket] Help with simple macro
On Wed, 5/16/12, Harry Spier <vasishtha.spier at gmail.com> wrote:
> I'm trying to create a macro to
> simplify the syntax of having a local
> scope with an indexed sequence generator.
>
> Instead of entering this:
> (let-values ([(more? get next) (sequence-generate
> (in-indexed SOME-SEQUENCE)])
> .... (get-next)
> ...(get-next).. etc. ))
>
> I'd like to be able to enter this instead:
> (with-sequence SOME-SEQUENCE
> . . . .. . (get-next) .
> . . . (get-next) . . . etc.)
>
> I'm able to create a macro to do this
> (with-sequence MORE? GET-NEXT SOME-SEQUENCE .........)
> i.e.
> (define-syntax-rule (with-indexed-sequence MORE? GET-NEXT
> SEQ BODY ...)
> (let-values ([(MORE?
> GET-NEXT)(sequence-generate (in-indexed SEQ))])
> BODY ... ))
>
> but I've been unable to create a macro to create this form:
> ---------------------------------------
> (with-sequence SOME-SEQUENCE
> . . . .. . (get-next) .
> . . . (get-next) . . . etc.)
> ---------------------------------------
>
> where I don't pass the names of the thunks to test for end
> of sequence
> or to get the next item in the sequence, but that they are
> called
> "more?" and "get-next" by convention.
>
> How would I do this.
Can you instead simply use a for loop?
Something like
(for ([(x i) (in-indexed '("and" "so" "forth"))]
#:when ((string-length x) . < . 4))
(displayln (list x i)))