[racket] Problem with macro
Thanks Danny,
I've changed the syntax of the macro as follows by faking keyword
arguments prompt: and handler: and it works now as far as I can tell.
I saw some discussion on the dev list about keyword parameters and
syntax but I didn't quite follow it, so I wasn't sure if it was saying
that keyword parameters in macros wll or won't be implemented.
;let/set-prompt is let type syntax for call-with-continuation-prompt
procedure.
;(let/set-prompt ([prompt: prompt] [handler: handler] args) body)
; prompt and handle are optional
; Examples:
; (let/set-prompt () (+ 1 2 3 4))
; (let/set-prompt ([prompt: prompt-1]) (+ 1 2 3 4))
; (let/set-prompt ([prompt: prompt-1] [handler: handler-1]) (+ 1 2 3 4))
; (let/set-prompt ([prompt: prompt-1] [handler: handler-1] [val 4]) (+
1 2 3 val))
(define-syntax let/set-prompt
(syntax-rules (prompt: handler:)
[(_ ((prompt: PROMPT) (handler: HANDLER) ARGS ...) BODY ...)
(call-with-continuation-prompt (let (ARGS ...) (λ () BODY ...)) PROMPT
HANDLER)]
[(_ ((handler: HANDLER) (prompt: PROMPT) ARGS ...) BODY ...)
(call-with-continuation-prompt (let (ARGS ...) (λ () BODY ...)) PROMPT
HANDLER)]
[(_ ((prompt: PROMPT) ARGS ...) BODY ...)
(call-with-continuation-prompt (let (ARGS ...) (λ () BODY ...))
PROMPT)]
[(_ ((handler: HANDLER) ARGS ...) BODY ...)
(call-with-continuation-prompt (let (ARGS ...) (λ () BODY ...))
PROMPT)]
[(_ ( ARGS ...) BODY ...)
(call-with-continuation-prompt (let (ARGS ...) (λ () BODY ...)))]))
(define-syntax abort-to-prompt
(syntax-rules ()
[(_ VAL) (abort-current-continuation
(default-continuation-prompt-tag) (thunk VAL))]
[(_ PROMPT-TAG VAL) (abort-current-continuation PROMPT-TAG (thunk VAL))]))
(define prompt-1 (make-continuation-prompt-tag))
(define prompt-2 (make-continuation-prompt-tag))
(define prompt-3 (make-continuation-prompt-tag))
(define (play-with-prompts [p (default-continuation-prompt-tag)])
(+ 1
(let/set-prompt ([prompt: prompt-1] [val 10] ) (+ val
(let/set-prompt ([prompt: prompt-2] [val 100]) (+ val
(let/set-prompt ([prompt: prompt-3] [val 1000]) (+ val
(abort-to-prompt p 0) 2000)) 200)) 20))))
(play-with-prompts prompt-1)
(play-with-prompts prompt-2)
(play-with-prompts prompt-3)
(play-with-prompts)
On Sun, Sep 2, 2012 at 12:27 AM, Danny Yoo <dyoo at hashcollision.org> wrote:
>> (let/set-prompt prompt-3 ()
>> (+ 1000
>> (abort-to-prompt p 0)
>> 2000))
>>
>> ==>
>>
>> (call-with-continuation-prompt
>> (let () (λ ()
>> +
>> 1000
>> (abort-to-prompt p 0)
>> 2000))
>> prompt-3)
>>
>>
>>
>> And whoops! That's where we're getting the funky values back: the use
>> of + just got ripped apart by the macro, by accident. We popped its
>> bubble, and the components just spilled out into the body of the let.
>
>
> Doh. Typo. I meant to say: "... and the components just spilled out
> into the body of the λ". Sorry about the confusion!