Always Move Fast - Fun with Interfacing Chicken Scheme and C

Fun with Interfacing Chicken Scheme and C

Follow me on Twitter @davidreynolds

In my last blog entry I briefly mentioned that I've been hacking a little bit with Chicken Scheme. I have a few more things in store, but for now I'd like to share a small bit of code that does one thing: generates a Scheme vector of pairs in C and passes that object back to Chicken Scheme. This code is relatively simple and will be used eventually for returning a vector of file descriptors and events associated with them. This code is a stepping stone into wrapping epoll_wait.

The documentation for Chicken Scheme is rather good, but there are quite a few blank spots in the "interfacing with C" category. The C implementation is pretty readable, so I was able to dig around there for a little bit and learn more about how to write a decent C interface to Chicken. If you're in the mood, I'd encourage you to take a look at the implementation and specifically runtime.c in the Chicken source directory.

This is the C file that defines a function for generating a vector of pairs:

and here's the Scheme file that interfaces with vector_of_pairs.c:

Compile these with: $ csc vector_of_pairs.c test.scm -o test and run the resulting executable. Oh, I should mention that this is on Linux, so your experience setting this up will be different unless you're on Linux as well. For information on how to install Chicken Scheme, visit this link.