views:

56

answers:

1

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 (lambda (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 :)

+2  A: 

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.

Nathan Sanders
Thanks, I think that's what I'm looking for. I've been reading through that page and it's a bit tough to digest; can you explain what the `syntax` procedure does? Also helpful would be an easier-to-understand description of `with-syntax`, and `datum->syntax`/`syntax->datum`.
Cam
Actually, nevermind - I know understand those terms. Thanks!
Cam
I can't edit that comment anymore, but it's still annoying me: I *now understand those terms :)
Cam