From de23343739a79331883cfd2bede0afc0504657c0 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 9 Feb 2019 14:29:34 +0000 Subject: [PATCH] Standardise Guile simple example Changes so that it works the same as other language modules --- Examples/guile/simple/example.c | 27 ++++++++++++--------------- Examples/guile/simple/example.i | 6 ++---- Examples/guile/simple/runme.scm | 28 ++++++++++++---------------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/Examples/guile/simple/example.c b/Examples/guile/simple/example.c index dcafc4dc4..1c2af789c 100644 --- a/Examples/guile/simple/example.c +++ b/Examples/guile/simple/example.c @@ -1,21 +1,18 @@ -/* Simple example from documentation */ /* File : example.c */ -#include +/* 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(<ime); - return ctime(<ime); -} diff --git a/Examples/guile/simple/example.i b/Examples/guile/simple/example.i index 1a9930a14..4fcea98b2 100644 --- a/Examples/guile/simple/example.i +++ b/Examples/guile/simple/example.i @@ -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 diff --git a/Examples/guile/simple/runme.scm b/Examples/guile/simple/runme.scm index c3fd0b41f..ccd755701 100644 --- a/Examples/guile/simple/runme.scm +++ b/Examples/guile/simple/runme.scm @@ -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))