From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.lisp.guile.devel Subject: Non-stack-copying call-with-current-continuation? Date: Fri, 02 Mar 2012 01:00:43 +0100 Organization: Organization?!? Message-ID: <87ty27eus4.fsf@fencepost.gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1330646468 16566 80.91.229.3 (2 Mar 2012 00:01:08 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 2 Mar 2012 00:01:08 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Fri Mar 02 01:01:06 2012 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1S3Fvk-00035N-Es for guile-devel@m.gmane.org; Fri, 02 Mar 2012 01:01:04 +0100 Original-Received: from localhost ([::1]:60102 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3Fvj-0001lk-Pl for guile-devel@m.gmane.org; Thu, 01 Mar 2012 19:01:03 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:58947) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3Fvf-0001kz-SX for guile-devel@gnu.org; Thu, 01 Mar 2012 19:01:01 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S3Fve-0003qZ-6T for guile-devel@gnu.org; Thu, 01 Mar 2012 19:00:59 -0500 Original-Received: from plane.gmane.org ([80.91.229.3]:37222) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3Fvd-0003qV-VS for guile-devel@gnu.org; Thu, 01 Mar 2012 19:00:58 -0500 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1S3FvZ-0002wt-Co for guile-devel@gnu.org; Fri, 02 Mar 2012 01:00:53 +0100 Original-Received: from p57b9e4ae.dip.t-dialin.net ([87.185.228.174]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 02 Mar 2012 01:00:53 +0100 Original-Received: from dak by p57b9e4ae.dip.t-dialin.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 02 Mar 2012 01:00:53 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 39 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: p57b9e4ae.dip.t-dialin.net X-Face: 2FEFf>]>q>2iw=B6, xrUubRI>pR&Ml9=ao@P@i)L:\urd*t9M~y1^:+Y]'C0~{mAl`oQuAl \!3KEIp?*w`|bL5qr,H)LFO6Q=qx~iH4DN; i"; /yuIsqbLLCh/!U#X[S~(5eZ41to5f%E@'ELIi$t^ Vc\LWP@J5p^rst0+('>Er0=^1{]M9!p?&:\z]|;&=NP3AhB!B_bi^]Pfkw User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux) Cancel-Lock: sha1:cwDCOmcMALae4QZNmHnfp/jc/TU= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:13972 Archived-At: Hi, I am just meddling around with coding and have come up with the following: (define-public (find-child music predicate) "Find the first node in @var{music} that satisfies @var{predicate}." (catch 'music-found (lambda () (fold-some-music predicate (lambda (music . _) (throw 'music-found music)) #f music)) (lambda (key music) music))) Now the problem with that is that it is unhygienic. If fold-some-music were to use music-found signals, or if the predicate did, things would be awkward. One would need to work with a uniquely generated symbol. It turns out that the above can be expressed much clearer and cleaner as (define-public (find-child music predicate) "Find the first node in @var{music} that satisfies @var{predicate}." (call-with-current-continuation (lambda (music-found) (fold-some-music predicate (lambda (music . _) (music-found music)) #f music)))) at least if I did not make some thinko here. It is basically the same code and stack-upwards-only, but hygienic. Nothing can call the continuation but what is inside. Well, of course fold-some-music could save the closure calling music-found for later. But it doesn't. Is there a way to get a call-with-current-continuation that does not create a stack copy? It is fine if it fails with an exception if one still tries calling the continuation after it has already returned. -- David Kastrup