Hi,
i have some functions def that allow returning early from function defined.
But i have a few difficulties to pass from the version 'define to the version 'lambda.
'def is defined this way (code shortened):
(module def racket/base
(provide def return return-rec)
(require srfi/31 ;; for 'rec in def*
racket/stxparam
(for-syntax racket/base))
(define-syntax def
(lambda (stx)
(syntax-case stx ()
((_ (<name> <arg> ...) <body> <body>* ...)
#'(define (<name> <arg> ...)
(call/cc
(lambda (ret-rec-id) ;(#,ret-rec-id)
;; In the body we adjust the 'return-rec' keyword so that calls
;; to 'return-rec' are replaced with calls to the escape
;; continuation.
(syntax-parameterize
([return-rec (syntax-rules ()
[(return-rec vals (... ...))
(ret-rec-id vals (... ...))])])
(apply (rec <name> (lambda (<arg> ...)
(call/cc
(lambda (ret-id) ;(#,ret-id)
;; In the body we adjust the 'return' keyword so that calls
;; to 'return' are replaced with calls to the escape
;; continuation.
(syntax-parameterize
([return (syntax-rules ()
[(return vals (... ...))
(ret-id vals (... ...))])])
<body> <body>* ...)))))
(list <arg> ...)))))))
))) ; check the number of parenthesis !
which works well allowing to 'return from the current call and 'return-rec from all recursive calls if any.
But i have problem to define a 'lambda+ that would do the same (code shortened but should works):
(define-syntax lambda+
(lambda (stx)
(syntax-case stx ()
((_ (<arg> ...) <body> <body>* ...)
#'(lambda (<arg> ...)
(apply (rec <name>
(lambda (<arg> ...)
(call/cc
(lambda (ret-id) ;(#,ret-id)
;; In the body we adjust the 'return' keyword so that calls
;; to 'return' are replaced with calls to the escape
;; continuation.
(syntax-parameterize
([return (syntax-rules ()
[(return vals (... ...))
(ret-id vals (... ...))])])
;;(display "def+.rkt : def+ : <body> =") (display <body>) (newline)
<body>
<body>*
...)))))
(list <arg> ...))))
)))
return-rec is not critical to my code, as it can be done by return in a program, and Python has just return and no way to return directly from all the nested calls but as it works in def i think there could be a solution for lambda+ ?
for now i can execute this:
(define x 3)
((rec
bar
(lambda+
()
(when (x < 5) (set!x (+ x 1)) (display "super!") (newline) (bar))
(display "returning")
(newline)
(return 17)
'not_good))))
super!
super!
returning
returning
returning
17
only at REPL at some point i got a solution with other code and return-rec with an expected output:
super!
super!
returning
17
but was at REPL and definitions have less restrictions than in module.
Regards,