Standardise Mzscheme simple example

Changes so that it works the same as other language modules
This commit is contained in:
William S Fulton 2019-02-09 14:43:27 +00:00
commit 30d0c16ac0
4 changed files with 37 additions and 46 deletions

View file

@ -1 +0,0 @@
Simple example from users manual.

View file

@ -1,24 +1,18 @@
/* Simple example from documentation */
/* File : example.c */ /* File : example.c */
#include <time.h> /* A global variable */
double Foo = 3.0;
double My_variable = 3.0; /* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
/* Compute factorial of n */ int g;
int fact(int n) { g = y;
if (n <= 1) return 1; while (x > 0) {
else return n*fact(n-1); g = x;
x = y % x;
y = g;
} }
return g;
/* Compute n mod m */
int my_mod(int n, int m) {
return (n % m);
} }
char *get_time() {
long ltime;
time(&ltime);
return ctime(&ltime);
}

View file

@ -1,16 +1,7 @@
/* File : example.i */ /* File : example.i */
%module example %module example
%{
/* Put headers and other declarations here */
%}
%include typemaps.i
%rename(mod) my_mod;
%inline %{ %inline %{
extern double My_variable; extern int gcd(int x, int y);
extern int fact(int); extern double Foo;
extern int my_mod(int n, int m);
extern char *get_time();
%} %}

View file

@ -2,23 +2,30 @@
(load-extension "example.so") (load-extension "example.so")
(display (get-time)) ; Call our gcd() function
(printf "My-variable = ~a~n" (My-variable)) (define x 42)
(define y 105)
(let loop ((i 0)) (define g (gcd x y))
(when (< i 14) (begin (display i) (display "The gcd of ")
(display " factorial is ") (display x)
(display (fact i)) (display " and ")
(display y)
(display " is ")
(display g)
(newline) (newline)
(loop (+ i 1)))))
(let loop ((i 1)) ; Manipulate the Foo global variable
(when (< i 250)
(begin
(let loopi ((j 1))
(when (< j 250) (begin (My-variable (+ (My-variable) (mod i j)))
(loopi (+ j 1)))))
(loop (+ i 1)))))
(printf "My-variable = ~a~n" (My-variable)) ; Output its current value
(display "Foo = ")
(display (Foo))
(newline)
; Change its value
(Foo 3.1415926)
; See if the change took effect
(display "Foo = ")
(display (Foo))
(newline)