[Top][All Lists]

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

Re: escaping from a recursive call

From: Zelphir Kaltstahl
Subject: Re: escaping from a recursive call
Date: Wed, 9 Nov 2022 18:56:28 +0000

Hello Damien,

On 11/9/22 18:18, Damien Mattei 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....)

On Wed, Nov 9, 2022 at 5:52 PM Olivier Dion<>  wrote:

On Wed, 09 Nov 2022, Damien Mattei<>  wrote:
i need a way to escape not only the current call of a recursive function
(it is already done) but alls:


(def (foo n)
      (cond ((= n 0) 'end0)
   ((= n 7) (return 'end7))
   (else (cons n (foo {n - 1})))))
Is that what you want?
--8<---------------cut here---------------start------------->8---
  (ice-9 control))

(define (foo n)
   (let/ec return
     (let loop ((n n))
        ((= n 0) 'end0)
        ((= n 7) (return 'end7))
         (cons n (loop (1- n))))))))

(pk (foo 5))
(pk (foo 10))
--8<---------------cut here---------------end--------------->8---

Olivier Dion

Another thing you could do is to pass the continuation to the function you are calling explicitly. I built a small example, which is not really a real-world use-case, but hopefully explains the idea:

(import (except (rnrs base))
        (only (guile)
              lambda* λ

(define factorial-with-continuation
  (lambda* (n
            ;; Explicitly specifying an argument, which is a
            ;; continuation.
            ;; By default the continuation cont is merely
            ;; returning the value given. Identity
            ;; function. Not doing anything with the result.
            (cont (λ (x) x))
            ;; Using `remainder` only as an example.
            (test (λ (num) (= (remainder num 7) 0))))
    (let iter ([num° (- n 1)]
               [product° n])
       ;; Base case of factorial.
       [(<= num° 1) product°]
       ;; Next a case with some condition, which, if true
       ;; means you want to exit.
       [(test num°)
        (cont product°)]
       ;; Otherwise normal iteration.
        (iter (- num° 1)
              (* product° num°))]))))

(factorial-with-continuation 10)
(factorial-with-continuation 10
                             ;; A continuation, which merely
                             ;; display something, but then
                             ;; returns the value.
                             (λ (num)
                               (display (simple-format #f "~a\n" num))
                             ;; Another weird condition.
                             (λ (num) (= (remainder num 17) 0)))

In this case the continuation only displays something, but you could make it do whatever you want, including using its result inside the function you are calling, instead of returning it. Also you can define the continuation before calling the function `factorial-with-continuation`.

This might not be the most convenient way, since you have an additional argument, but maybe it is a very general way of doing something like that.



reply via email to

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