This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: (lambda (foo) (some-c-function)) - can that C function get foo ?


Martin Baulig <martin@home-of-linux.org> writes:

> Hello guys,
> 
> This is a strange subject for this mail, but it's also some "strange"
> problem.
> 
> I'm using some C code like this:
> 
> ====
> static SCM
> test (void)
> {
>   return gh_eval_str ("foo");
> }
> 
> void
> scm_init_gdb_gdb_module ()
> {
>   gh_new_procedure ("testfunc", test, 0, 0, 0);
> }
> ====
> 
> Then I try calling it in guile like this:
> 
> ====
> (let ((foo "bar")) (testfunc))
> ====
> 
> all I get is an "Unbound variable: foo" error. Same for the following:
> 
> ====
> (define bar (lambda (foo) (testfunc)))
> (bar 10)
> ====
> 
> This works without problems when I define "foo" in the top-level env
> like this:
> 
> ====
> guile> (define foo "bar")
> guile> (testfunc)
> "bar"
> ====
> 
> Is this a guile bug or am I doing something wrong ?

It's working as advertised, gh_eval_str evaluates in the top-level
environment. There are other functions to evaluate in the local
environment, but are you sure you really need to do that?

> In general, when you call a C function inside a (let ...) or a (lambda ...)
> expressions is it possible to access the variables that are defined locally
> in the scope of that expression ?
> 

Well, why do you want to do that anyway? Surely there is a cleaner
solution to your problem.

 - Maciej




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]