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: Sat, 12 Jul 2008 13:47:24 +0200 (CEST)



On Fri, 11 Jul 2008, Maciek Godek wrote:

Kjetil S. Matheussen:

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)))

I don't actually get the line with c-display.
Does it require any additional module?
'Cause it ain' workin' for me.
(and besides what does it do?
the remaining part works fine)


Just ignore that one. c-display is just a debugging
function I forgot to remove. :-)


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

(++)
=> 20

I'm really impressed :) Great respect!

I only wonder if there's a way to add a new
variable to an existing closure, or to delete one.
(not that I need it right now; but I imagine that
it can be implemented quite easily and efficiently
in C if the notion of closure is well-defined and
explicit)


Adding is easy:

(define (add-var-to-environment env name val)
  (local-eval `(let ((,name ,val)) (the-environment))
              env))

(define env (add-var-to-environment (the-environment) 'a 50))

(local-eval 'a env)
=> 50


I don't know if there's possible to delete a variable
in a clean way.




reply via email to

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