[gambit-list] callback from C to user defined function in Gambit

Michele Zaffalon michele.zaffalon at gmail.com
Sat Sep 26 09:19:19 EDT 2009


Hi,

I wanted to use GSL from Gambit but I am stuck at the point in which I
need to pass a user defined function to some of the GSL procedures.
Some take a data type gsl_function defined in the following way:
double (* function) (double x, void * params).
This is what I have done so far. (Not to add any suspense, I have to
say it doesn't work.)
My minimal solution consists in passing an int (*f)(int) function from
Gambit to a the library function that takes as arguments a pointer to
a function f and an integer and does nothing else than applying f to
x.

I am now getting confused by the indirection:

(define gsl-func
  (c-lambda ( (function (int) int) int) int "gsl_func"))

is the Scheme binding to the C library function and

(c-define (helper-func f x)
	  ( (function (int) int) int) int "helper_func" ""
	  (f x))

is the helper function that will be called by the gsl procedure. This
is the gsl procedure (which belongs to a shared library)

int gsl_func(int (*f)(int), int x)
{
  int result = helper_func(f, x);  //here result is f(x)
  return do_something(result);
}

>From Scheme, I would call
(gsl-func (lambda (x) (some-function-that-returns-an-int x)) an-int),
and gsl-func would in turn call the helper function which calls the lambda form.
Now when I compile and run it, gsi complains with

*** ERROR IN (console)@30.1 -- (Argument 1) Can't convert to C function
(gsl-func '#<procedure #8 f> 4)

where 4 is the integer.

I found the following
https://mercure.iro.umontreal.ca/pipermail/gambit-list/2009-March/003296.html
but it doesn't allow to define more than one function at the time.
How can I do it?

michele



More information about the Gambit-list mailing list