guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: escaping from a recursive call


From: Chris Vine
Subject: Re: escaping from a recursive call
Date: Thu, 10 Nov 2022 12:32:44 +0000

On Wed, 09 Nov 2022 12:55:42 -0500
Olivier Dion via General Guile related discussions <guile-user@gnu.org>
wrote:
> On Wed, 09 Nov 2022, Damien Mattei <damien.mattei@gmail.com> 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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]