[gambit-list] Help with Gambit Scheme re: modularity.

Marc Feeley feeley at iro.umontreal.ca
Sun Jul 5 19:42:21 EDT 2009


On 5-Jul-09, at 5:10 PM, Pierre Bernatchez wrote:

> My goal is to maximize the portability of my scheme source code.
>
> Here is what I think I should be doing:
>
> Confine most of my code to r5rs scheme, and, with srfi-0
> support, use SRFI's when I need them.
> Similarly use the full gsi scheme language on occasion when I need
> that.
>
> Presuming gsi is a superset of r5rs, I could just write everything
> in that language and use self discipline, but It would be nice to get
> support from the system inhibiting me from stepping outside the
> bounds of r5rs in a given module.


Here's how you can do that.  Write your code like this (the first two  
lines are what is important):

(##namespace ("not-r5rs#")) ;; prefix to use on all global names
(##include "~~lib/r5rs#.scm") ;; except the R5RS ones

(define (foo)
   (display "hello")
   (newline))

(define (bar)
   (println "world"))

(foo)
(bar)

If you call procedures that are not in r5rs (like the Gambit specific  
procedure "println" in the above code) then you will get an error  
message.  For example this is the output when running the above code  
(which is stored in the file "limited-to-r5rs.scm"):

% gsi limited-to-r5rs.scm
hello
*** ERROR IN not-r5rs#bar, "limited-to-r5rs.scm"@9.4 -- Unbound  
variable: not-r5rs#println

Use (##include "~~lib/r4rs#.scm") if you want to limit to R4RS or  
(##include "~~lib/gambit#.scm") to get all the Gambit goodness.

Note that this does not catch all portability problems, because some  
"standard" procedures may have implementation dependent behavior.  So  
for example in Gambit (sqrt 9) will evaluate to 3 (the exact integer  
3).  Some implementations of Scheme will evaluate this to 3.0 (an  
inexact real).  Although there are many such discrepancies, they  
seldom cause problems (but when there is a problem it can be very hard  
to debug!).

Marc




More information about the Gambit-list mailing list