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 */
#include <time.h>
/* A global variable */
double Foo = 3.0;
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
/* 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;
}
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 %{
extern double My_variable;
extern int fact(int);
extern int mod(int n, int m);
extern char *get_time();
extern int gcd(int x, int y);
extern double Foo;
%}
%include guile/guilemain.i

View file

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