> On Jun 2, 2019, at 9:41 PM, Kevin Forchione <lyss...@gmail.com> wrote:
> 
> Hi guys,
> I’ve been working with macros and let/cc and have a situation where I have 
> the 2nd macro bound to the let/cc return of the first. No idea how I’d do 
> that, but here’s an example of what I’m attempting to do:
> 
> #lang racket
> 
> (require (for-syntax racket/syntax))
> 
> (define-syntax (fn stx)
>  (syntax-case stx ()
>    [(_ (arg ...) body0 body ...)
>     (with-syntax ([return (format-id #'body0 "~a" #'return)])
>       #'(λ (arg ...) (let/cc return body0 body ...)))]))
> 
> (define-syntax (false stx)
>  (syntax-case stx ()
>    [(_) #'(return #f)]))
> 
> ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
> ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
> 
> My thought is that the 2nd macro doesn’t know about the 1st macro except 
> perhaps through the six of compile time… so that if I had define-syntax 
> working with a lambda I’d have all the pieces available in the (fn …) but not 
> sure what tools I might use to accomplish that.
> 
> Any ideas are appreciated!
 

This situation calls for syntax-parameters in my mind: see docs, too — Matthias



#lang racket

(require (for-syntax ) racket/stxparam)

(define-syntax-parameter return (syntax-rules ()))

(define-syntax fn
  (syntax-rules ()
    [(_ (arg ...) body ...)
     (λ (arg ...)
       (let/cc fn-exit
         (syntax-parameterize ([return (syntax-rules () [(_ x) (fn-exit x)])])
           body ...)))]))

(define-syntax false
  (syntax-rules ()
    [(_) (return #f)]))


((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/7A495B17-F49A-41BE-8DED-8EC34D4FE61F%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to