From: "Kjetil S. Matheussen" <k.s.matheussen@notam02.no>
To: Clinton Ebadi <clinton@unknownlamer.org>, guile-user@gnu.org
Subject: Re: Injecting variables into closures.
Date: Thu, 29 Nov 2007 16:52:08 +0100 (CET) [thread overview]
Message-ID: <Pine.LNX.4.64.0711291647360.9048@ttleush> (raw)
In-Reply-To: <877ik1dpyj.fsf@unknownlamer.org>
On Thu, 29 Nov 2007, Clinton Ebadi wrote:
> "Kjetil S. Matheussen" <k.s.matheussen@notam02.no> 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
next prev parent reply other threads:[~2007-11-29 15:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.64.0711291647360.9048@ttleush \
--to=k.s.matheussen@notam02.no \
--cc=clinton@unknownlamer.org \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).