From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: escaping from a recursive call Date: Wed, 9 Nov 2022 18:56:28 +0000 Message-ID: <6fbea690-e51b-567e-238d-16061ab83e5b@posteo.de> References: <8735asoytw.fsf@laura> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14000"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: Damien Mattei Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Wed Nov 09 19:57:15 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 1osqGA-0003SW-Ny for guile-user@m.gmane-mx.org; Wed, 09 Nov 2022 19:57:14 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1osqFb-0001uG-6H; Wed, 09 Nov 2022 13:56:39 -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 1osqFZ-0001ts-0Y for guile-user@gnu.org; Wed, 09 Nov 2022 13:56:37 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1osqFW-0004XI-Ik for guile-user@gnu.org; Wed, 09 Nov 2022 13:56:36 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 06100240026 for ; Wed, 9 Nov 2022 19:56:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1668020190; bh=nqEi+mHJNbTsLiv+mYPYNnXjdzOzp9ThvSYWf4zuubQ=; h=Date:Subject:To:From:Cc:From; b=H7hPISOH1LrabsXgVZYXvaeMwWugEv5YTJbi+CH7pOmkrF0A2AeAhExNXuNx/UIJ7 8SHbdUG4k1s3+cfcz8Px0ai/0LwwqjrGDZ1gWP3md6mAHPh9Fx5YKGdEKHvntAfHEX C5d4ycNxucfoooYlzGLfmBKCdvt+P2ID1xfZU1WRdUhRs79RIedCIcrQPT1o2XAL5C NadC5PZtluzevKCpbOhD/ytECJyxQsBz4lzE3m6Hmy+6ZwZOm5q0oaCLqQrfLTQXdY u+JsqTw5N68bjMAtSl43Et8xf8x8neSrKgERZEa/dPpb2ppT/NwGw7B7B5PfF+yB7o jSNlqyiBdYhzA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4N6vMJ6zqrz9rxL; Wed, 9 Nov 2022 19:56:28 +0100 (CET) Content-Language: en-US In-Reply-To: Received-SPF: pass client-ip=185.67.36.65; envelope-from=zelphirkaltstahl@posteo.de; helo=mout01.posteo.de 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, 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:18705 Archived-At: 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: >>> >>> example: >>> >>> (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--- >> (use-modules >> (ice-9 control)) >> >> (define (foo n) >> (let/ec return >> (let loop ((n n)) >> (cond >> ((= n 0) 'end0) >> ((= n 7) (return 'end7)) >> (else >> (cons n (loop (1- n)))))))) >> >> (pk (foo 5)) >> (pk (foo 10)) >> --8<---------------cut here---------------end--------------->8--- >> >> -- >> Olivier Dion >> oldiob.dev 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* λ display simple-format)) (define factorial-with-continuation (lambda* (n #:optional ;; 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]) (cond ;; 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. [else (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)) 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. Regards, Zelphir -- repositories:https://notabug.org/ZelphirKaltstahl