unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* escaping from a recursive call
@ 2022-11-09 15:56 Damien Mattei
  2022-11-09 16:51 ` Olivier Dion via General Guile related discussions
  0 siblings, 1 reply; 13+ messages in thread
From: Damien Mattei @ 2022-11-09 15:56 UTC (permalink / raw)
  To: guile-user

i need a way to escape not only the current call of a recursive function
(it is already done) but alls:

example:

(def (foo n)
     (cond ((= n 0) 'end0)
  ((= n 7) (return 'end7))
  (else (cons n (foo {n - 1})))))

cheme@(guile-user)> (foo 5)
(5 4 3 2 1 . end0)
scheme@(guile-user)> (foo 10)
(10 9 8 . end7)

it has just escaped from the current call to foo

(def (bar n)
     (cond ((= n 0) 'end0)
  ((= n 7) (return-rec 'end7))
  (else (cons n (bar {n - 1})))))

scheme@(guile-user)> (bar 5)
(5 4 3 2 1 . end0)
scheme@(guile-user)> (bar 10)
ice-9/boot-9.scm:1669:16: In procedure raise-exception:
Unbound variable: return-rec

i admit i should read the K. Dybvig macro paper but still had not
completely do it.

i modify this code of my Scheme+ but it fails on (bar 10) :

(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> ...)


      (define (<name> <arg> ...)
(call/cc (lambda (#,ret-id) <body> <body>* ...)))

      (call/cc (lambda (#,ret-rec-id)
 (<name> <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"))

)))

i'am even surprised it works on (bar 5)

if someone had a solution to this problem i will again copy/paste it in my
code and gracefully thanks him a lot :-)

best regards,
damien


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-11-10 17:03 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09 15:56 escaping from a recursive call Damien Mattei
2022-11-09 16:51 ` Olivier Dion via General Guile related discussions
2022-11-09 17:18   ` Damien Mattei
2022-11-09 17:55     ` Olivier Dion via General Guile related discussions
2022-11-09 18:49       ` Damien Mattei
2022-11-10  1:20         ` Olivier Dion via General Guile related discussions
2022-11-10  4:56           ` tomas
2022-11-10  6:01             ` Damien Mattei
2022-11-10  5:25           ` Damien Mattei
2022-11-10 17:03             ` Olivier Dion via General Guile related discussions
2022-11-10 12:32       ` Chris Vine
2022-11-10 13:48         ` Damien Mattei
2022-11-09 18:56     ` Zelphir Kaltstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).