[plt-scheme] define-for-syntax mutual recursion

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Sun May 31 23:38:22 EDT 2009

On Sun, May 31, 2009 at 11:27 PM, Anthony Cowley <acowley at seas.upenn.edu> wrote:
> I believe this is a variation of a past question, but I don't think it
> was completely answered...
>
> Is there any way to have mutually recursive functions defined via
> define-for-syntax?
>
> When writing run-time code, I can write
> (define (foo x) (bar x))
> (define (bar x) (+ x 2))
>
> and that's fine. But I can't write,
> (define-for-syntax (foo x) (bar x))
> (define-for-syntax (bar x) (+ x 2))
>
> In this example, I could of course re-order the definitions, but if
> they call each other then I seem to be out of luck. I would be happier
> not having to move these guys into another module, but at the very
> least I'd like to have a better understanding why such a construction
> is disallowed.
>
> Can someone shed some light on this for me?
>
> Thanks,
> Anthony

You can use define-values-for-syntax:

(define-values-for-syntax [is-even? is-odd?]
  (values
    (lambda (x) (if (zero? x) #t (is-odd? (sub1 x))))
    (lambda (x) (if (zero? x) #f (is-even? (sub1 x))))))

-- 
Carl Eastlund


Posted on the users mailing list.