From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Kjetil S. Matheussen" Newsgroups: gmane.lisp.guile.user Subject: Re: Closure? Date: Sat, 12 Jul 2008 13:47:24 +0200 (CEST) Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7BIT X-Trace: ger.gmane.org 1215863282 9542 80.91.229.12 (12 Jul 2008 11:48:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 12 Jul 2008 11:48:02 +0000 (UTC) Cc: guile-user@gnu.org, "Kjetil S. Matheussen" To: Maciek Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Jul 12 13:48:49 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KHdar-0000hp-Ao for guile-user@m.gmane.org; Sat, 12 Jul 2008 13:48:49 +0200 Original-Received: from localhost ([127.0.0.1]:49204 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KHdZz-00043Y-Cu for guile-user@m.gmane.org; Sat, 12 Jul 2008 07:47:55 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KHdZu-00043O-Ui for guile-user@gnu.org; Sat, 12 Jul 2008 07:47:51 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KHdZt-00042w-05 for guile-user@gnu.org; Sat, 12 Jul 2008 07:47:50 -0400 Original-Received: from [199.232.76.173] (port=43160 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KHdZs-00042l-Qa for guile-user@gnu.org; Sat, 12 Jul 2008 07:47:48 -0400 Original-Received: from smtp.getmail.no ([84.208.20.33]:33066) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KHdZs-0004bZ-DM for guile-user@gnu.org; Sat, 12 Jul 2008 07:47:48 -0400 Original-Received: from pmxchannel-daemon.no-osl-m323-srv-009-z2.isp.get.no by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) id <0K3W006016337X00@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Sat, 12 Jul 2008 13:47:27 +0200 (CEST) Original-Received: from smtp.getmail.no ([10.5.16.1]) by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0K3W001SK6310O30@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Sat, 12 Jul 2008 13:47:25 +0200 (CEST) Original-Received: from cm-84.215.136.96.getinternet.no ([84.215.136.96]) by no-osl-m323-srv-004-z1.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0K3W00GKP631NWQ0@no-osl-m323-srv-004-z1.isp.get.no> for guile-user@gnu.org; Sat, 12 Jul 2008 13:47:25 +0200 (CEST) In-reply-to: X-X-Sender: kjetil@ttleush X-detected-kernel: by monty-python.gnu.org: Solaris 10 (beta) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6645 Archived-At: On Fri, 11 Jul 2008, Maciek Godek wrote: > Kjetil S. Matheussen: > >> The function "local-eval" is probably what you want. >> >> I use local-eval a lot, it's one of the really really >> nice features provided by Guile: >> >> >> (define-macro (with env . code) >> `(local-eval (quote (begin ,@code)) ,env)) >> >> >> (define ++ >> (let ((c 0)) >> (c-display "env" (the-environment)) >> (lambda() >> (set! c (1+ c)) c))) > > I don't actually get the line with c-display. > Does it require any additional module? > 'Cause it ain' workin' for me. > (and besides what does it do? > the remaining part works fine) > Just ignore that one. c-display is just a debugging function I forgot to remove. :-) >> (++) >> => 1 >> (with (procedure-environment ++) (set! c 20)) >> >> (++) >> => 20 > > I'm really impressed :) Great respect! > > I only wonder if there's a way to add a new > variable to an existing closure, or to delete one. > (not that I need it right now; but I imagine that > it can be implemented quite easily and efficiently > in C if the notion of closure is well-defined and > explicit) > Adding is easy: (define (add-var-to-environment env name val) (local-eval `(let ((,name ,val)) (the-environment)) env)) (define env (add-var-to-environment (the-environment) 'a 50)) (local-eval 'a env) => 50 I don't know if there's possible to delete a variable in a clean way.