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: Injecting variables into closures. Date: Thu, 29 Nov 2007 16:52:08 +0100 (CET) Message-ID: References: <877ik1dpyj.fsf@unknownlamer.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Trace: ger.gmane.org 1196351561 28550 80.91.229.12 (29 Nov 2007 15:52:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 29 Nov 2007 15:52:41 +0000 (UTC) To: Clinton Ebadi , guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Nov 29 16:52:49 2007 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 1Ixlgt-00079i-TM for guile-user@m.gmane.org; Thu, 29 Nov 2007 16:52:40 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ixlge-0001Wx-Fe for guile-user@m.gmane.org; Thu, 29 Nov 2007 10:52:24 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IxlgY-0001TW-Pl for guile-user@gnu.org; Thu, 29 Nov 2007 10:52:19 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IxlgW-0001QC-OU for guile-user@gnu.org; Thu, 29 Nov 2007 10:52:17 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IxlgW-0001Pv-Cq for guile-user@gnu.org; Thu, 29 Nov 2007 10:52:16 -0500 Original-Received: from mail-forward.uio.no ([129.240.10.42]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1IxlgV-0000H7-DR for guile-user@gnu.org; Thu, 29 Nov 2007 10:52:15 -0500 Original-Received: from mail-mx8.uio.no ([129.240.10.38]) by pat.uio.no with esmtp (Exim 4.67) (envelope-from ) id 1IxlgT-0000DL-5w; Thu, 29 Nov 2007 16:52:13 +0100 Original-Received: from bjo1-1x-dhcp290.studby.uio.no ([193.157.245.38]) by mail-mx8.uio.no with esmtps (TLSv1:AES256-SHA:256) (Exim 4.67) (envelope-from ) id 1IxlgR-0002Jj-LA; Thu, 29 Nov 2007 16:52:11 +0100 X-X-Sender: kjetil@ttleush In-Reply-To: <877ik1dpyj.fsf@unknownlamer.org> X-UiO-ClamAV-Virus: No X-UiO-Spam-info: not spam, SpamAssassin (score=-0.0, required=12.0, autolearn=disabled, AWL=-0.019) X-UiO-Scanned: 8F4CC74D13463F5F3111BD8E0D721DBAB5B6E4F2 X-UiO-SPAM-Test: remote_host: 193.157.245.38 spam_score: 0 maxlevel 200 minaction 2 bait 0 mail/h: 1 total 539 max/h 6 blacklist 0 greylist 0 ratelimit 0 X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) 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:6305 Archived-At: On Thu, 29 Nov 2007, Clinton Ebadi wrote: > "Kjetil S. Matheussen" writes: > >> Hi, >> >> I need (well) to inject variables into closures, and have >> come up with the following macro to do so: >> >> (define-macro (inject-variable name value) >> (define env (gensym)) >> (define closure (gensym)) >> (define vars (gensym)) >> (define secondname (gensym)) >> (define secondval (gensym)) >> (define first-env (gensym)) >> `((procedure->macro (lambda (,(gensym) ,env) >> (cond ((null? (cdr ,env)) >> (let ((,closure (car ,env))) >> (set-car! ,env (cons ',name ,value)) >> (set-cdr! ,env (list ,closure)))) >> ((null? (null? (cdr (car ,env)))) >> (let* ((,vars (car ,env)) >> (,secondname (car vars)) >> (,secondval (cdr vars))) >> (set-car! ,env >> (list (list ',name firstname) >> ,value ,secondval)))) >> (else >> (let ((,first-env (car ,env))) >> (set-cdr! ,first-env >> (cons ,value (cdr ,first-env))) >> (set-car! ,first-env >> (cons ',name (car ,first-env))) >> (set-car! ,env ,first-env)))))))) >> >> >> (let () >> (display (defined? 'gakk)) >> (inject-variable gakk 90) >> gakk) >> => #f90 >> >> >> Now I wonder, is this safe and portable? Or are there >> other and better ways to do this? > > Manipulating the environment is not going to be portable or safe. In Well, it doesn't have to be. But I understand very well why its not. :-) > addition procedure->macro should be deprecated (but appears not to > be?), and will be removed (necessarily) whenever someone finishes the > phase separation in the evaluator. > Hope its not going to be. Its a convenient function. > Is there a special reason to inject a value into the environment > rather than let binding it? > I'm just playing with making a prettier module system: (define-namespace bank) (def-bank sum 0) (def-bank (inc n) (inc! sum n)) (def-bank (dec n) (inc sum (- n))) (bank/inc 50) (bank/sum) => 50 I can also do it by doing a complete source transformations, replacing sum with bank/sum everywhere and so on, but I would rather avoid doing that. Its just for fun. _______________________________________________ Guile-user mailing list Guile-user@gnu.org http://lists.gnu.org/mailman/listinfo/guile-user