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: Injecting variables into closures. Date: Thu, 29 Nov 2007 15:04:12 +0100 (CET) Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Trace: ger.gmane.org 1196345080 4168 80.91.229.12 (29 Nov 2007 14:04:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 29 Nov 2007 14:04:40 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Nov 29 15:04:48 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 1Ixk0M-0001Pf-V6 for guile-user@m.gmane.org; Thu, 29 Nov 2007 15:04:39 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ixk07-0001xi-GJ for guile-user@m.gmane.org; Thu, 29 Nov 2007 09:04:23 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ixk02-0001x5-AA for guile-user@gnu.org; Thu, 29 Nov 2007 09:04:18 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ixk00-0001wg-RB for guile-user@gnu.org; Thu, 29 Nov 2007 09:04:17 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ixk00-0001wc-LL for guile-user@gnu.org; Thu, 29 Nov 2007 09:04: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 1Ixjzz-00049G-N6 for guile-user@gnu.org; Thu, 29 Nov 2007 09:04:16 -0500 Original-Received: from mail-mx6.uio.no ([129.240.10.47]) by pat.uio.no with esmtp (Exim 4.67) (envelope-from ) id 1Ixjzy-0005HL-AX for guile-user@gnu.org; Thu, 29 Nov 2007 15:04:14 +0100 Original-Received: from bjo1-1x-dhcp290.studby.uio.no ([193.157.245.38]) by mail-mx6.uio.no with esmtps (TLSv1:AES256-SHA:256) (Exim 4.67) (envelope-from ) id 1Ixjzy-00030d-19 for guile-user@gnu.org; Thu, 29 Nov 2007 15:04:14 +0100 X-X-Sender: kjetil@ttleush X-UiO-ClamAV-Virus: No X-UiO-Spam-info: not spam, SpamAssassin (score=0.0, required=12.0, autolearn=disabled, none) X-UiO-Scanned: 991057DA31DDE9790067408D82CC946943538E0E X-UiO-SPAM-Test: remote_host: 193.157.245.38 spam_score: 0 maxlevel 200 minaction 2 bait 0 mail/h: 1 total 535 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:6302 Archived-At: 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? Thanks for any answers. Kjetil. _______________________________________________ Guile-user mailing list Guile-user@gnu.org http://lists.gnu.org/mailman/listinfo/guile-user