Next: , Previous: Lambda, Up: Procedures and Macros


5.8.2 Primitive Procedures

Procedures written in C can be registered for use from Scheme, provided they take only arguments of type SCM and return SCM values. scm_c_define_gsubr is likely to be the most useful mechanism, combining the process of registration (scm_c_make_gsubr) and definition (scm_define).

— Function: SCM scm_c_make_gsubr (const char *name, int req, int opt, int rst, fcn)

Register a C procedure FCN as a “subr” — a primitive subroutine that can be called from Scheme. It will be associated with the given name but no environment binding will be created. The arguments req, opt and rst specify the number of required, optional and “rest” arguments respectively. The total number of these arguments should match the actual number of arguments to fcn. The number of rest arguments should be 0 or 1. scm_c_make_gsubr returns a value of type SCM which is a “handle” for the procedure.

— Function: SCM scm_c_define_gsubr (const char *name, int req, int opt, int rst, fcn)

Register a C procedure FCN, as for scm_c_make_gsubr above, and additionally create a top-level Scheme binding for the procedure in the “current environment” using scm_define. scm_c_define_gsubr returns a handle for the procedure in the same way as scm_c_make_gsubr, which is usually not further required.

scm_c_make_gsubr and scm_c_define_gsubr automatically use scm_c_make_subr and also scm_makcclo if necessary. It is advisable to use the gsubr variants since they provide a slightly higher-level abstraction of the Guile implementation.