Standardise Guile 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:29:34 +00:00
commit de23343739
3 changed files with 26 additions and 35 deletions

View file

@ -1,21 +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) {
int fact(int n) { int g;
if (n <= 1) return 1; g = y;
else return n*fact(n-1); while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
} }
int mod(int n, int m) {
return (n % m);
}
char *get_time() {
long ltime;
time(&ltime);
return ctime(&ltime);
}

View file

@ -5,10 +5,8 @@
%} %}
%inline %{ %inline %{
extern double My_variable; extern int gcd(int x, int y);
extern int fact(int); extern double Foo;
extern int mod(int n, int m);
extern char *get_time();
%} %}
%include guile/guilemain.i %include guile/guilemain.i

View file

@ -3,24 +3,20 @@
(for-each display args) (for-each display args)
(newline)) (newline))
(mdisplay-newline (get-time) "My variable = " (My-variable)) ; Call our gcd() function
(do ((i 0 (1+ i))) (define x 42)
((= 14 i)) (define y 105)
(mdisplay-newline i " factorial is " (fact i))) (define g (gcd x y))
(mdisplay-newline "The gcd of " x " and " y " is " g)
(define (mods i imax j jmax) ; Manipulate the Foo global variable
(if (< i imax)
(if (< j jmax)
(begin
(My-variable (+ (My-variable) (mod i j)))
(mods i imax (+ j 1) jmax))
(mods (+ i 1) imax 1 jmax))))
(mods 1 150 1 150) ; Output its current value
(mdisplay-newline "Foo = " (Foo))
(mdisplay-newline "My-variable = " (My-variable)) ; Change its value
(Foo 3.1415926)
(exit (and (= 1932053504 (fact 13))
(= 745470.0 (My-variable))))
; See if the change took effect
(mdisplay-newline "Foo = " (Foo))