From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: Re: escaping from a recursive call Date: Wed, 9 Nov 2022 19:49:07 +0100 Message-ID: References: <8735asoytw.fsf@laura> <87y1sknhb5.fsf@laura> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20650"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: Olivier Dion Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Wed Nov 09 19:50:26 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1osq9Y-00053c-DV for guile-user@m.gmane-mx.org; Wed, 09 Nov 2022 19:50:24 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1osq8Z-00085r-Pc; Wed, 09 Nov 2022 13:49:23 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1osq8Y-00085e-5Q for guile-user@gnu.org; Wed, 09 Nov 2022 13:49:22 -0500 Original-Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1osq8W-0006zW-0i for guile-user@gnu.org; Wed, 09 Nov 2022 13:49:21 -0500 Original-Received: by mail-ej1-x629.google.com with SMTP id m22so12111821eji.10 for ; Wed, 09 Nov 2022 10:49:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=bXCiSBcFErj/yzBGI3qts+WkQs09vNKr5Wyoxu84dl0=; b=UNukyCApV8feeNC1gurREzAVWRw7tngX+JVT4QZ1gdYlV1qENI3OlI2MAKv7iqiRIO GkdjomHVW6S1XLzw8wtM/H0znGanEITGHSk8aasYN9q7UifnS+Ft2SQ0oHNI+3EHx6OG 9sZ0Qgf5h12DcVGOirnwsRXJj9cimUJAQ7n5fIpHDVfpVCH6tsvwpxfCecHn794RUecN NZ6kFWMI+ThtoP863D/07KgS0Sk6ZIHcFgHgXRqVCJcCXXYjj56ZxlcNCI75DZzNs1LS eUlbCEWSdM746BXHhcetqqkyJNYPibvtcYmHD0irS7B5Fl5jMs8aPnEGFknwFKWRyRz3 HlYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bXCiSBcFErj/yzBGI3qts+WkQs09vNKr5Wyoxu84dl0=; b=hgylcDyAOwO2yitmgk2b26nyogt+4kVg394vRnBbrGJDyQObG/1o/+VGtCI1ChK4/F HLM6Ofuvk3AxetRT8RoWK1X8nIqk+x5S3LcnVAHzEOBw39/k0zqlE9YcV6wkLJby3nC3 CMcQpp7TNqY079/TH0qL6c0Qm2w3wksIbxpvpJuGa46HjrGPZX0KkegaFbN4d8QalKK3 q2rF/opNuXVYv9tEm+eCl6uK5l/5MQDjC0VPgjr2NX+e6icNPbNf39e9r2QFDjtCCDog FC3/iZU7taNGvXpJslYmpbOv+5IKvbYkaJO9mCHqOjuJZi1p0vIw+Ak0X/qYRk8I8onq 3J8g== X-Gm-Message-State: ANoB5plTxyNsNypXWoJaTAsIBRcedO9z0DwXtaK82KKf7659h63wbW9f FASUwAgb/Bwo47whlVeHk5mRcmspI5jPHNb/ZUk= X-Google-Smtp-Source: AA0mqf4OCg1KmD17Jffqagoig2VT3mvAlOhwq/z2ocZfk1BhriUVeSiT5F38IKjTjY6ROl5R4kOAtIR4aqAgvDVmY8I= X-Received: by 2002:a17:907:98a3:b0:7ae:5e8f:ac70 with SMTP id ju3-20020a17090798a300b007ae5e8fac70mr18569613ejc.529.1668019758157; Wed, 09 Nov 2022 10:49:18 -0800 (PST) In-Reply-To: <87y1sknhb5.fsf@laura> Received-SPF: pass client-ip=2a00:1450:4864:20::629; envelope-from=damien.mattei@gmail.com; helo=mail-ej1-x629.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:18704 Archived-At: 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")) ;; ((_ ( ...) * ...) ;; (let ((ret-id (datum->syntax stx 'return))) ;; #`(define ( ...) ;; (call/cc (lambda (#,ret-id) * ...))))) ((_ ( ...) * ...) (let ((ret-id (datum->syntax stx 'return)) (ret-rec-id (datum->syntax stx 'return-rec))) #`(define ( ...) (call/cc (lambda (#,ret-rec-id) (apply (rec (lambda ( ...) (call/cc (lambda (#,ret-id) * ...)))) (list ...))))))) ;; 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 =3D (5 4 3 2 1 . end0) ;; scheme@(guile-user)> (foo 10) ;; $3 =3D (10 9 8 . end7) ;; scheme@(guile-user)> (bar 5) ;; $4 =3D (5 4 3 2 1 . end0) ;; scheme@(guile-user)> (bar 10) ;; $5 =3D end7 (def (foo n) (cond ((=3D n 0) 'end0) ((=3D n 7) (return 'end7)) (else (cons n (foo {n - 1}))))) (def (bar n) (cond ((=3D n 0) 'end0) ((=3D n 7) (return-rec 'end7)) (else (cons n (bar {n - 1}))))) the important part of this macro being: ((_ ( ...) * ...) (let ((ret-id (datum->syntax stx 'return)) (ret-rec-id (datum->syntax stx 'return-rec))) #`(define ( ...) (call/cc (lambda (#,ret-rec-id) (apply (rec (lambda ( ...) (call/cc (lambda (#,ret-id) * ...)))) (list ...))))))) but i admit i had a chance to find this solution, i did it a bit like a blind man... but after all: =E2=80=9CA mathematician is a blind man in a dark room looking for a black = cat which isn=E2=80=99t there.=E2=80=9D -- Charles Darwin Best regards, Damien On Wed, Nov 9, 2022 at 6:55 PM Olivier Dion wrote= : > On Wed, 09 Nov 2022, Damien Mattei wrote: > > but in the general case , i want a macro that can do it on any functio= n > > (i'm not sure it can be done because the continuation have to be captur= ed > > 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 > ((=3D n 0) 'end0) > ((=3D 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 >