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 ?


> The problem is that I can't pass any additional parameters to the callback
> function (so I can't call scm_eval_3 () with another environment), but I
> think I found a good solution for this problem (see below).

You can, you use a closure for this. The point is that using the (lambda)
expression allows you to bind variables into that lambda expression and it
will remember where those variables came from (it won't lookup the variables
by name when it executes but will use them based on what they mean at the
place the lambda expression is written). This means that a thunk can contain
as many extra parameters as you like even though it has no formal parameters.

This is one of those really cool scheme features that doesn't translate well
into traditional languages because it depends on the compile-as-you-go feature
of the interpreter. It is really convenient to use but takes a while to get
used to if you come from an background in C, FORTRAN or whatever. The (lambda)
is a bit like a mini-compiler that gives you back a chunk of code, thus a
scheme program with lambda expressions is effectively a self-modifying program.
Now if we went around calling it ``self-modifying'' people would not like it
because they have been taught ``bad'' when they hear that so we say ``closure''
and teach them that closure is ``good''.

> > > 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.
> 
> Hmm, I think I found a solution for it.
> 
> I'll define some reasonable defaults for the callbacks in a module global
> variable and use that variable in the C code.

Yeah, good old global variables -- theorists have worked hard to kill
them but somehow they always come back, probably because the work so well.
Or as the FORTRAN programmers say, ``I sure love my COMMON block!''.

	- Tel


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