From: "Maciek Godek" <pstrychuj@gmail.com>
To: guile-user@gnu.org
Subject: Closure?
Date: Fri, 11 Jul 2008 16:48:06 +0200 [thread overview]
Message-ID: <e2ceda030807110748w62e5573ao191a3bbb2073ffc8@mail.gmail.com> (raw)
Hi folks,
I've been wondering if there's any way to recall
(or get inside) an environment of a closure (= to
directly access variables bound to a closure)
And here's what I mean exactly:
(define ++ (let ((c 0)) (lambda()(set! c (1+ c))c)))
; we now have a closure
(procedure-environment ++)
;-> ((c . 0) #<eval-closure 7f6d672efa80>)
(++)
;-> 1
; Now I would like to be able to do the following:
(with (procedure-environment ++) (set! c 20))
; the c variable in closure ++ is set to 20
(define -- (with (procedure-environment ++)
(lambda()(set! c (1- c)) c)))
; here we have a new function bound to the same closure
(--)
;-> 19
Is there any way to achieve this in guile?
(it is especially important for me to make it
efficiently doable from C). I've tried some
funny combinations of eval and procedure-environment,
but they didn't work out.
Additionaly, it would be nice to see the possibility
of explicit definitions of environments, like:
(define env (make-closure (a . 1)(b . 2))
(with env (define c 3))
so that we could define the aforementioned
counter as:
(define counter-env (make-closure (c . 0)))
(define ++ (with counter-env (lambda()(set! c (1+ c))c)))
and so forth.
By the way, the let special form could then be
defined using this make-environment function
(define-macro (let bindings body)
(with (make-closure* bindings) body))
(I've suffixed the name with asterisk as some
transformations between the let bindings and
make-closure bindings are required to make it
work properly)
Hope I'm not reinventing the wheel here; it
would be nice to be able to use guile in such
way, though (and to make sure that it is
efficient from the C level as well). It
would also make scheme oop more intuitive,
as method names could be hidden inside a closure
and so it would allow the following syntax:
(with object (method args))
which corresponds to the famous dot syntax
known from many oo-languages like c++ or python:
object.method(args)
Would it be difficult to implement such
functionality in guile? (if there's none implemented yet
-- I didn't find anything in the documentation)
tia4i
(thanks in advance for implementation :)
Maciek Godek
next reply other threads:[~2008-07-11 14:48 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-11 14:48 Maciek Godek [this message]
2008-07-11 15:01 ` Closure? Ludovic Courtès
2008-07-11 15:32 ` Closure? Maciek Godek
2008-07-12 22:57 ` Closure? Neil Jerram
2008-07-13 6:57 ` Closure? Maciek Godek
2008-07-13 22:56 ` Closure? Neil Jerram
2008-07-14 1:15 ` Closure? Maciek Godek
[not found] <cmu-lmtpd-26382-1215792454-10@mail-imap1.uio.no>
2008-07-11 17:42 ` Closure? Kjetil S. Matheussen
2008-07-11 17:47 ` Closure? Kjetil S. Matheussen
2008-07-11 20:54 ` Closure? Maciek Godek
2008-07-12 11:47 ` Closure? Kjetil S. Matheussen
2008-07-13 6:59 ` Closure? Maciek Godek
2008-07-12 22:43 ` Closure? Neil Jerram
2008-07-15 7:59 ` Closure? Ludovic Courtès
2008-07-15 9:11 ` Closure? Andy Wingo
2008-07-16 16:42 ` Closure? Ludovic Courtès
[not found] <cmu-lmtpd-15105-1216051603-3@mail-imap1.uio.no>
2008-07-14 16:30 ` Closure? Kjetil S. Matheussen
2008-07-14 21:14 ` Closure? Maciek Godek
[not found] ` <e2ceda030807141414i5acef7d1h37d12d14e01cc1d@mail.gmail.com>
2008-07-14 21:41 ` Closure? Kjetil S. Matheussen
2008-07-14 22:46 ` Closure? Maciek Godek
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=e2ceda030807110748w62e5573ao191a3bbb2073ffc8@mail.gmail.com \
--to=pstrychuj@gmail.com \
--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).