[plt-scheme] Re: Introducing an Identifier into the Lexical Context of a Macro Cal l

From: David Van Horn (dvanhorn at cs.uvm.edu)
Date: Wed Oct 13 13:09:24 EDT 2004

Williams, M. Douglas wrote:
> The with-syntax is more appealing.  In my case, I am generating a let to
> introduce a variable around a body of code from the macro call.  I assume
> that generating the identifier in the context of the body would address the
> macros expanding to non-hygienic macros issue (at least in most cases).
> Is there a good example anywhere of similar code in PLT Scheme?  Generating
> a 'self' variable for class methods would be an example that comes to mind.

I'm not sure exactly what you're asking for.  If you write down what you'd 
like to be able to express, it's easy to derive a macro for it, though.

But here is an example that does what I think you want.  It defines a special 
form fix-lambda that is like lambda, but binds the identifier `fix' in the 
body of the function to the function itself.

(define-syntax (fix-lambda stx)
   (syntax-case stx ()
     ((fix-lambda spec body ...)
      (with-syntax ((fix (datum->syntax-object (syntax fix-lambda) 'fix)))
         (letrec ((fix (lambda spec body ...))) fix))))))

;; factorial of 5
((fix-lambda (i) (if (zero? i) 1 (* i (fix (sub1 i))))) 5) ;; 120


Posted on the users mailing list.