GNU Info

Info Node: (guile-tut.info)c_builtins.c

(guile-tut.info)c_builtins.c


Next: What learn1 is doing Prev: c_builtins.h Up: More interesting programming with libguile
Enter node , (file) or (file)node

c_builtins.c
------------

  Here is `c_builtins.c':
     #include <stdio.h>
     #include <math.h>
     
     #include <guile/gh.h>
     
     #include "c_builtins.h"
     
     /* this is a factorial routine in C, made to be callable by Scheme */
     SCM c_factorial(SCM s_n)
     {
       int i;
       unsigned long result = 1, n;
     
       n = gh_scm2ulong(s_n);
     
       gh_defer_ints();
       for (i = 1; i <= n; ++i) {
         result = result*i;
       }
       gh_allow_ints();
       return gh_ulong2scm(result);
     }
     
     /* a sin routine in C, callable from Scheme.  it is named c_sin() to
        distinguish it from the default Scheme sin function */
     SCM c_sin(SCM s_x)
     {
       double x = gh_scm2double(s_x);
     
       return gh_double2scm(sin(x));
     }
     
     /* play around with vectors in Guile: this routine creates a vector of
        the given length, initializes it all to zero except element 2 which
        is set to 1.9.  */
     SCM vector_test(SCM s_length)
     {
       SCM xvec;
     
       c_length = gh_scm2ulong(s_length);
       printf("requested length for vector: %ld\n", gh_scm2ulong(s_length));
     
       /* create a vector */
       xvec = gh_make_vector(s_length, gh_double2scm(0.0));
       /* set the second element in it */
       gh_vector_set_x(xvec, gh_int2scm(2), gh_double2scm(1.9));
     
       return xvec;
     }


automatically generated by info2www version 1.2.2.9