unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Injecting variables into closures.
@ 2007-11-29 14:04 Kjetil S. Matheussen
  2007-11-29 14:44 ` Kjetil S. Matheussen
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Kjetil S. Matheussen @ 2007-11-29 14:04 UTC (permalink / raw)
  To: guile-user


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


^ permalink raw reply	[flat|nested] 7+ messages in thread
[parent not found: <cmu-lmtpd-10256-1196356001-0@mail-imap2.uio.no>]

end of thread, other threads:[~2007-11-29 18:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-29 14:04 Injecting variables into closures Kjetil S. Matheussen
2007-11-29 14:44 ` Kjetil S. Matheussen
2007-11-29 14:54 ` Kjetil S. Matheussen
2007-11-29 16:02   ` Ludovic Courtès
     [not found] ` <877ik1dpyj.fsf@unknownlamer.org>
2007-11-29 15:52   ` Kjetil S. Matheussen
2007-11-29 15:53 ` Ludovic Courtès
     [not found] <cmu-lmtpd-10256-1196356001-0@mail-imap2.uio.no>
2007-11-29 18:43 ` Kjetil S. Matheussen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).