From 91068e40857ae938428b19095c1a791b00269e1b Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Wed, 10 Aug 2011 13:18:03 -0400 Subject: [PATCH] adding example to the prose about using (for-syntax (for-syntax ...)) --- collects/scribblings/guide/proc-macros.scrbl | 33 +++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/collects/scribblings/guide/proc-macros.scrbl b/collects/scribblings/guide/proc-macros.scrbl index af40482fda..622421bd1b 100644 --- a/collects/scribblings/guide/proc-macros.scrbl +++ b/collects/scribblings/guide/proc-macros.scrbl @@ -445,7 +445,38 @@ the right-hand side of the inner @racket[define-syntax] is in the @deftech{meta-compile phase level}, also known as @deftech{phase level 2}. To import @racket[syntax-case] into that phase level, you would have to use @racket[(require (for-syntax (for-syntax racket/base)))] -or, equivalently, @racket[(require (for-meta 2 racket/base))]. +or, equivalently, @racket[(require (for-meta 2 racket/base))]. For example, +@codeblock|{ +#lang racket/base +(require ;; This provides the bindings for the definition + ;; of shell-game. + (for-syntax racket/base) + + ;; And this for the definition of + ;; swap. + (for-syntax (for-syntax racket/base))) + +(define-syntax (shell-game stx) + + (define-syntax (swap stx) + (syntax-case stx () + [(_ a b) + #'(let ([tmp a]) + (set! a b) + (set! b tmp))])) + + (syntax-case stx () + [(_ a b c) + (let ([a #'a] [b #'b] [c #'c]) + (when (= 0 (random 2)) (swap a b)) + (when (= 0 (random 2)) (swap b c)) + (when (= 0 (random 2)) (swap a c)) + #`(list #,a #,b #,c))])) + +(shell-game 3 4 5) +(shell-game 3 4 5) +(shell-game 3 4 5) +}| Negative phase levels also exist. If a macro uses a helper function that is imported @racket[for-syntax], and if the helper function