Something like this:

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

(define-syntax (let-cbn stx)
  (syntax-parse stx
    [(_let-cbn ([x:id e:expr] ...) body)
     ;; For each identifier x we need a new identifier bound to (λ () e)
     (define/with-syntax (x* ...) (generate-temporaries #'(x ...)))
     (syntax/loc stx
       (let ([x* (λ () e)] ...)
         ;; in the boby we need to rewrite x to (x*)
         ;; we need to handle references, applications and assignments
         (let-syntax ([x (λ (so)
                           (syntax-parse so
                             #:literals (set!)
                             [_x:id         (syntax/loc so (x*))]
; reference to x*
                             [(_x:id e ...) (syntax/loc so ((x*) e ...))]
; application
                              [(set! _ __)                  ; assignment
                               (raise-syntax-error
                                'let-cbn "assignment to cbn variable not
allowed" #'stx so)]))]
                      ...)
           body)))]))

> (let-cbn ([x 0] [y (/ 1 0)]) x)
0

> (let-cbn ([x 0] [y (/ 1 0)]) y)
/: division by zero


2015-07-29 15:28 GMT+02:00 Klaus Ostermann <klaus...@gmail.com>:

> 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?
>
> --
> 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.
>



-- 
-- 
Jens Axel Søgaard

-- 
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