On Wed, 09 Nov 2022 12:55:42 -0500
Olivier Dion via General Guile related discussions <[email protected]>
wrote:
> On Wed, 09 Nov 2022, Damien Mattei <[email protected]> wrote:
> > but in the general case , i want a macro that can do it on any function
> > (i'm not sure it can be done because the continuation have to be captured
> > just before the call to the function and be inlined at the good
> > place....)
>
> I'm not aware of any control mechanism that are implicit in Guile. You
> almost always have to deal with a continuation object. However, nothing
> prevent you to invent your own control flow wrapper.
You can construct an anaphoric macro with that in mind. This introduces
an imperative-style 'loop' macro which carries within the loop block a
'break' keyword which will cause the loop to exit:
(use-modules (ice-9 control)) ;; for call/ec
(define-syntax loop
(lambda (x)
(syntax-case x ()
[(k e ...)
(with-syntax ([break (datum->syntax #'k 'break)])
#'(call/ec
(lambda (break)
(let f () e ... (f)))))])))
(display (let ([n 3] [lst '()])
(loop
(if (= n 0) (break lst))
(set! lst (cons 'a lst))
(set! n (- n 1)))))
(newline)
However explicit control of loops is better in my view. Imperative
loops usually end up with mutable bindings, as in the example above.