开发者

How to make just part of a macro hygienic

开发者 https://www.devze.com 2023-01-07 04:04 出处:网络
I\'d like to have a version of lambda, called lambda-r, from within which you can return. An example:

I'd like to have a version of lambda, called lambda-r, from within which you can return. An example:

(+ ((lambda-r ()
    (return 1)
    2)) 5)

This would give the value 6. Although you might expect the value to be 7, it's 6 because 1 is returned from the lambda-expression before 2 is reached.

Here's an example of the kind of transformation I'm looking for. Let's say one were to use lambda-r as follows:

(lambda-r (a b)
    (return a)
    (+ a b))

I want it to be transformed like this:

(call/cc (lamb开发者_StackOverflow社区da (k)
       (define return (lambda (v)
                        (k (lambda (a b)
                             v))))
       (lambda (a b)
         (return a)
         (+ a b))))

That could also be expressed with a let-expression instead of an internal define, but I'm using the define for clarity's sake.

Note that the above code does actually work as expected. The problem is that I'm having trouble expressing lambda-r as a macro. The reason is that I want k and v to be hygienic, but I don't want return to be hygienic.

My macro at the moment is this:

(define-syntax lambda-r
  (syntax-rules (return)
    [(_ (var ...) body ...)
     (call/cc (lambda (k)
           (define return (lambda (v)
                            (k (lambda (var ...)
                                 v))))
           (lambda (var ...)
             body ...)))
     ]))

Which doesn't work because return is treated hygienically, and as a result isn't directly visible when using lambda-r. So (lambda-r () (return 1)) gives an error which says that return isn't a valid identifier.


Edit: Thanks to Nathan Sanders' answer, I'm closer to understanding what I have to do here. However I don't fully understand the following procedures, and as a result haven't been able to get this working yet. I'd really appreciate if you could explain/direct me to resources which explain the following:

  • the syntax procedure
  • the datum->syntax/syntax->datum procedures

Edit: Nevermind - I've got it now :)


What you want is syntax-case instead of syntax-rules.

The R6RS definition gives some examples, notably a section on syntax-object and datum conversions, which is what you want. You should be able to adapt the loop w/ break example to your return.

0

精彩评论

暂无评论...
验证码 换一张
取 消