At Wed, 29 Jul 2015 06:28:48 -0700 (PDT), Klaus Ostermann wrote:
> I'd like to have a macro "let-cbn" which does this:
> 
> (let-cbn ((x1 e1) ...) body)
> 
> is transformed to
> 
> (let ((x1 (thunk e1)) ...) newbody)
> 
> where newbody is the result of replacing every occurence of x1... by (x1)... .
> 
> What is the best way to do that in Racket?

I'd expand

 (let-cbn ((x1 e1) ...) body)

to

 (let ((x1 (thunk e1)) ...)
   (let-syntax ([x1 (make-thunk-call #'x1)] ...)
      body))

where `make-thunk-call` generates a macro that expands to a call of the
given argument:

 (define-for-syntax (make-thunk-call id-stx)
    (lambda (stx)
      (with-syntax ([id id-stx])
        (syntax-case stx ()
          [(_ arg ...) (syntax/loc stx (id arg ...))]
          [_ #'(id)]))))

A complete example is below.

----------------------------------------

#lang racket/base
(require (for-syntax racket/base))

(define-for-syntax (make-thunk-call id-stx)
  (lambda (stx)
    (with-syntax ([id id-stx])
      (syntax-case stx ()
        [(_ arg ...) (syntax/loc stx (id arg ...))]
        [_ #'(id)]))))

(define-syntax-rule (thunk e0 e ...)
  (lambda () e0 e ...))

(define-syntax-rule (let-cbn ((x1 e1) ...) body0 body ...)
  (let ((x1 (thunk e1)) ...)
    (let-syntax ([x1 (make-thunk-call #'x1)] ...)
      body0 body ...)))

(let-cbn ([x (begin (displayln "here") 1)])
  (list x x))

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to