From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Noah Lavine Newsgroups: gmane.lisp.guile.devel Subject: Re: Non-stack-copying call-with-current-continuation? Date: Thu, 1 Mar 2012 20:01:30 -0500 Message-ID: References: <87ty27eus4.fsf@fencepost.gnu.org> <87mx7zesuw.fsf@fencepost.gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1330650106 7183 80.91.229.3 (2 Mar 2012 01:01:46 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 2 Mar 2012 01:01:46 +0000 (UTC) Cc: guile-devel@gnu.org To: David Kastrup Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Fri Mar 02 02:01:45 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 1S3GsP-0004tp-HI for guile-devel@m.gmane.org; Fri, 02 Mar 2012 02:01:41 +0100 Original-Received: from localhost ([::1]:46849 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3GsO-00023w-Vu for guile-devel@m.gmane.org; Thu, 01 Mar 2012 20:01:40 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:56075) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3GsM-00023b-3Y for guile-devel@gnu.org; Thu, 01 Mar 2012 20:01:39 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S3GsK-0006ur-69 for guile-devel@gnu.org; Thu, 01 Mar 2012 20:01:37 -0500 Original-Received: from mail-iy0-f169.google.com ([209.85.210.169]:51447) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3GsG-0006tR-Nd; Thu, 01 Mar 2012 20:01:32 -0500 Original-Received: by iajr24 with SMTP id r24so1916369iaj.0 for ; Thu, 01 Mar 2012 17:01:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=PbucNYEpKdP+1lZjW7181svnMoW5RVvl5ksl+B7zMxI=; b=FN20zeakcaWfw3jpWMqQtgC3D7U55k2FXs/J7KFBJLg1oKYzbgJ085lpAArdu7BMhr c3N/g5xJuZZawd9pwpbZhrJi6shMJ3X91MJIyUFL/2e0Sqr9jE9dD19WLmoT1QL154PZ Yahn1Hgk250vgkqYncZ6dRhwxyemE9HIzZwgHK++UZTc/pzPHnjIiPIh98Y/fiOxa57k y2eOkCUTYJtzfpn8UsomQRY5lawmwTIrCCRVXVXxWvgUUr2KW5cvzx//1P8ERt6y1kKj N+MMxnGkwx8vcM+iBlVn680ehWC3eC90E+jiJi7t7bW1InqbjJ4+TSlv13vjrtVpq+i5 Qr0w== Original-Received: by 10.50.178.8 with SMTP id cu8mr116166igc.12.1330650090104; Thu, 01 Mar 2012 17:01:30 -0800 (PST) Original-Received: by 10.231.16.199 with HTTP; Thu, 1 Mar 2012 17:01:30 -0800 (PST) In-Reply-To: <87mx7zesuw.fsf@fencepost.gnu.org> X-Google-Sender-Auth: 882zte5dtaIYLFy-50uOPOyRfpw X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.210.169 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:13975 Archived-At: Oh yes, you're right. I'm sorry I missed it. I believe you can do it hygienically though. With prompts, you can use (make-prompt-tag) to generate a new, unique tag. With catch and throw, you could use (gensym) to do the same thing. You first example would become something like (define-public (find-child music predicate) "Find the first node in @var{music} that satisfies @var{predicate}." (let ((music-found-tag (gensym))) (catch music-found-tag (lambda () (fold-some-music predicate (lambda (music . _) (throw music-found-tag mus= ic)) #f music)) (lambda (key music) music))) Does that work? Noah On Thu, Mar 1, 2012 at 7:42 PM, David Kastrup wrote: > Noah Lavine writes: > >> On Thu, Mar 1, 2012 at 7:00 PM, David Kastrup wrote: >>> >>> Hi, >>> >>> I am just meddling around with coding and have come up with the >>> following: >>> >>> (define-public (find-child music predicate) >>> =A0"Find the first node in @var{music} that satisfies @var{predicate}." >>> =A0(catch 'music-found >>> =A0 =A0 =A0 =A0 (lambda () >>> =A0 =A0 =A0 =A0 =A0 (fold-some-music predicate >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(lambda (music .= _) (throw 'music-found music)) >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#f music)) >>> =A0 =A0 =A0 =A0 (lambda (key music) music))) >>> >>> Now the problem with that is that it is unhygienic. =A0If fold-some-mus= ic >>> were to use music-found signals, or if the predicate did, things would >>> be awkward. =A0One would need to work with a uniquely generated symbol. >>> It turns out that the above can be expressed much clearer and cleaner a= s >>> >>> (define-public (find-child music predicate) >>> =A0"Find the first node in @var{music} that satisfies @var{predicate}." >>> =A0(call-with-current-continuation >>> =A0 (lambda (music-found) >>> =A0 =A0 (fold-some-music predicate >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(lambda (music . _) (music-f= ound music)) >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#f music)))) >>> >>> at least if I did not make some thinko here. =A0It is basically the sam= e >>> code and stack-upwards-only, but hygienic. =A0Nothing can call the >>> continuation but what is inside. =A0Well, of course fold-some-music cou= ld >>> save the closure calling music-found for later. =A0But it doesn't. >>> >>> Is there a way to get a call-with-current-continuation that does not >>> create a stack copy? =A0It is fine if it fails with an exception if one >>> still tries calling the continuation after it has already returned. >> >> If I understand correctly, you only need to call the continuation >> once. Is that right? >> >> In that case, I think you could use either catch and throw or prompts. > > Neither of which are hygienic and also less straightforward. =A0It is not > like I did not start my posting by giving the catch/throw-based example > and saying what I disliked about it. > > -- > David Kastrup > >