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 ?


Telford Tendys <telford@eng.uts.edu.au> writes:

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

Hmm, and when I embed compiled C code in such a lambda impression and have
this C code call out to scheme, is it possible to make that scheme code be
compiled into the same lambda block (so I'll effectively have one single
lambda block with both scheme and C code in it) ?

> 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!''.

Well, I think they're good to hold some reasonable defaults for the callbacks.
Unlike fortran or C it is very easy in scheme to "locally modify" such global
variables in a dynamic wind thunk.

-- 
Martin Baulig - martin@home-of-linux.org - http://www.home-of-linux.org

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