Implement return function

I am trying to implement a return function in an R6RS schema. I want something like that:

 (lambda () (do-some-job-before) (return some-value) (do-some-job-after)) 

executes (do-some-job-before) , does not (do-some-job-after) and the final value of the lambda function in some-value .

I think I should use the sequel. I tried:

 (define return #f) (call/cc (lambda (k) (set! return k))) 

but it does not work; eg

 (+ 2 (return 3)) ; -> 3 (and not 5 as I expected) 

How can i do this?

+4
source share
1 answer

Edited: Invalid question.

Very simple:)

 (call/cc (lambda (return) (printf "before\n") (return 3) (printf "after\n"))) 

An example is here .

Note. . You cannot generalize this, unless you transfer it to the syntax from an unhygienic macro.

+5
source

All Articles