[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: escaping from a recursive call
From: |
Damien Mattei |
Subject: |
Re: escaping from a recursive call |
Date: |
Wed, 9 Nov 2022 19:49:07 +0100 |
good... thank, it works, i do not know a lot about 'prompts , a good
explanation is here:
https://stackoverflow.com/questions/29838344/what-exactly-is-a-continuation-prompt
i personally find a solution too:
(define-syntax def
(lambda (stx)
(syntax-case stx ()
;; multiple definitions without values assigned
;; (def (x y z))
((_ (var1 ...)) #`(begin (define var1 '()) ...))
;; (def (foo) (when #t (return "hello") "bye"))
;; ((_ (<name> <arg> ...) <body> <body>* ...)
;; (let ((ret-id (datum->syntax stx 'return)))
;; #`(define (<name> <arg> ...)
;; (call/cc (lambda (#,ret-id) <body> <body>* ...)))))
((_ (<name> <arg> ...) <body> <body>* ...)
(let ((ret-id (datum->syntax stx 'return))
(ret-rec-id (datum->syntax stx 'return-rec)))
#`(define (<name> <arg> ...)
(call/cc (lambda (#,ret-rec-id)
(apply (rec <name> (lambda (<arg> ...)
(call/cc (lambda (#,ret-id) <body> <body>* ...)))) (list <arg>
...)))))))
;; single definition without a value assigned
;; (def x)
((_ var) #`(define var '()))
;; (def x 7)
((_ var expr) #`(define var expr))
((_ err ...) #`(syntax-error "Bad def form"))
)))
example:
;; scheme@(guile-user)> (foo 5)
;; $2 = (5 4 3 2 1 . end0)
;; scheme@(guile-user)> (foo 10)
;; $3 = (10 9 8 . end7)
;; scheme@(guile-user)> (bar 5)
;; $4 = (5 4 3 2 1 . end0)
;; scheme@(guile-user)> (bar 10)
;; $5 = end7
(def (foo n)
(cond ((= n 0) 'end0)
((= n 7) (return 'end7))
(else (cons n (foo {n - 1})))))
(def (bar n)
(cond ((= n 0) 'end0)
((= n 7) (return-rec 'end7))
(else (cons n (bar {n - 1})))))
the important part of this macro being:
((_ (<name> <arg> ...) <body> <body>* ...)
(let ((ret-id (datum->syntax stx 'return))
(ret-rec-id (datum->syntax stx 'return-rec)))
#`(define (<name> <arg> ...)
(call/cc (lambda (#,ret-rec-id)
(apply (rec <name> (lambda (<arg> ...)
(call/cc (lambda (#,ret-id) <body> <body>* ...)))) (list <arg>
...)))))))
but i admit i had a chance to find this solution, i did it a bit like a
blind man...
but after all:
“A mathematician is a blind man in a dark room looking for a black cat
which isn’t there.”
-- Charles Darwin
Best regards,
Damien
On Wed, Nov 9, 2022 at 6:55 PM Olivier Dion <olivier.dion@polymtl.ca> 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.
>
> For example:
> --8<---------------cut here---------------start------------->8---
> (define my-prompt (make-prompt-tag))
>
> (define-syntax-rule (return-now x)
> (abort-to-prompt my-prompt x))
>
> (define (wrap-this procedure)
> (let ((inside? #f))
> (lambda args
> (if inside?
> (apply procedure args)
> (begin
> (set! inside? #t)
> (let ((ret
> (call-with-prompt my-prompt
> (lambda ()
> (apply procedure args))
> (lambda (_ x)
> x))))
> (set! inside? #f)
> ret))))))
>
> (define-syntax define-interruptible
> (syntax-rules ()
> ((_ (name formals ...) body ...)
> (define name
> (wrap-this
> (lambda (formals ...) body ...))))))
>
> (define-interruptible (foo n)
> (cond
> ((= n 0) 'end0)
> ((= n 7) (return-now 'end7))
> (else
> (cons n (foo (1- n))))))
>
> (pk (foo 5))
> (pk (foo 10))
> --8<---------------cut here---------------end--------------->8---
>
> There's probably other way of doing so that I'm not aware of.
>
> --
> Olivier Dion
> oldiob.dev
>