guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Closure?


From: Kjetil S. Matheussen
Subject: Re: Closure?
Date: Fri, 11 Jul 2008 19:42:40 +0200 (CEST)



Ludovic Court?s:
"Maciek Godek" <address@hidden> writes:

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)

Yes, with `the-environment':

 guile> ((lambda (a b) (the-environment)) 2 3)
 (((a b) 2 3) #<eval-closure b7c6dcf8>)

But don't do that, since the representation of environments could
eventually change.


Please don't change it. :-)
It's one of those things which makes Guile special.



Maciek Godek:

Don't worry, I won't :)
Now I only know how to obtain environment of a closure,
but I still don't know how to get inside and modify it (I'm
lacking this `with' I wrote about).


The function "local-eval" is probably what you want.

I use local-eval a lot, it's one of the really really
nice features provided by Guile:


(define-macro (with env . code)
  `(local-eval (quote (begin ,@code)) ,env))


(define ++
  (let ((c 0))
    (c-display "env" (the-environment))
    (lambda()
      (set! c (1+ c)) c)))

(++)
=> 1
(with (procedure-environment ++) (set! c 20))

(++)
=> 20






reply via email to

[Prev in Thread] Current Thread [Next in Thread]