Standardise Mzscheme simple example
Changes so that it works the same as other language modules
This commit is contained in:
parent
de23343739
commit
30d0c16ac0
4 changed files with 37 additions and 46 deletions
|
|
@ -1 +0,0 @@
|
|||
Simple example from users manual.
|
||||
|
|
@ -1,24 +1,18 @@
|
|||
/* Simple example from documentation */
|
||||
/* File : example.c */
|
||||
|
||||
#include <time.h>
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
double My_variable = 3.0;
|
||||
|
||||
/* Compute factorial of n */
|
||||
int fact(int n) {
|
||||
if (n <= 1) return 1;
|
||||
else return n*fact(n-1);
|
||||
}
|
||||
|
||||
/* Compute n mod m */
|
||||
int my_mod(int n, int m) {
|
||||
return (n % m);
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
char *get_time() {
|
||||
long ltime;
|
||||
time(<ime);
|
||||
return ctime(<ime);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,7 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
/* Put headers and other declarations here */
|
||||
%}
|
||||
|
||||
%include typemaps.i
|
||||
|
||||
%rename(mod) my_mod;
|
||||
|
||||
%inline %{
|
||||
extern double My_variable;
|
||||
extern int fact(int);
|
||||
extern int my_mod(int n, int m);
|
||||
extern char *get_time();
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
%}
|
||||
|
|
|
|||
|
|
@ -2,23 +2,30 @@
|
|||
|
||||
(load-extension "example.so")
|
||||
|
||||
(display (get-time))
|
||||
; Call our gcd() function
|
||||
|
||||
(printf "My-variable = ~a~n" (My-variable))
|
||||
(define x 42)
|
||||
(define y 105)
|
||||
(define g (gcd x y))
|
||||
(display "The gcd of ")
|
||||
(display x)
|
||||
(display " and ")
|
||||
(display y)
|
||||
(display " is ")
|
||||
(display g)
|
||||
(newline)
|
||||
|
||||
(let loop ((i 0))
|
||||
(when (< i 14) (begin (display i)
|
||||
(display " factorial is ")
|
||||
(display (fact i))
|
||||
(newline)
|
||||
(loop (+ i 1)))))
|
||||
; Manipulate the Foo global variable
|
||||
|
||||
(let loop ((i 1))
|
||||
(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)))))
|
||||
; Output its current value
|
||||
(display "Foo = ")
|
||||
(display (Foo))
|
||||
(newline)
|
||||
|
||||
(printf "My-variable = ~a~n" (My-variable))
|
||||
; Change its value
|
||||
(Foo 3.1415926)
|
||||
|
||||
; See if the change took effect
|
||||
(display "Foo = ")
|
||||
(display (Foo))
|
||||
(newline)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue