When you start Gauche, either in REPL or as a script, you’re in
user module, which inherits
Likewise, when you read a library, the initial module inherits
gauche module (until you call
That’s why you can access all the built-in procedures of Gauche
(use something). (See Module inheritance, for
the details about inheriting modules).
On the other hand, R7RS requires to be explicit about which namespaces
you’ll be using, by
import form, e.g.
(import (scheme base)).
Besides, R7RS library must be explicitly enclosed by
Before the first
import form of a program, or outside of
define-library, is beyond R7RS world—the standard defines
nothings about it.
These facts let Gauche to set up appropriate “world”, and you can use R7RS code and traditional Gauche code transparently.
NB: As explained in Three forms of import,
import is rather different from Gauche
import, so we note the former
r7rs#import and the latter
gauche#import in this section for clarity. When you write code
don’t use prefixes
gauche#; just write
define-library form is defined as a macro in
it sets up R7RS environment before evaluating its contents.
So, when you load an R7RS library (either from Gauche code via
form, or from R7RS code via
Gauche starts loading the file in
gauche module, but immediately
define-library form, and the rest is handled in R7RS
Suppose you have an R7RS library
(mylib foo) with the following code:
(define-library (mylib foo) (import (scheme base)) (export snoc) (begin (define (snoc x y) (cons y x))))
It should be saved as mylib/foo.scm in one of the directories in
From R7RS code, this library can be loaded by
(import (mylib foo)) (snoc 1 2) ⇒ (2 . 1)
To use this library from Gauche code, concatenate elements of library
. to get a module name, and
(use mylib.foo) (snoc 1 2) ⇒ (2 . 1)
To use Gauche library
foo.bar from R7RS code,
split the module name by
. to make a list for the name
of the library. For example,
gauche.lazy module can be
used from R7RS as follows:
(import (gauche lazy))
For SRFI modules, R7RS implementations have a convention to
name it as
(srfi n), and Gauche follows it.
The following code loads srfi-1 and srfi-13 from R7RS code:
(import (srfi 1) (srfi 13))
(It’s not that Gauche treat
srfi name specially; installation
of Gauche includes adapter libraries such as srfi/1.scm.)
A tip: To use Gauche’s built-in features (the bindings that are
available by default in Gauche code) from R7RS code, import
(gauche base) library (see Importing gauche built-ins):
(import (gauche base)) filter ⇒ #<closure filter>
R7RS scripts always begin with
import form. However,
has a different syntax and semantics
gauche#import—so we employ a trick.
gosh is started, it loads the given script file
user module. We have a separate
user#import macro, which
examines its arguments and if it is R7RS import syntax, switch to the
r7rs.user module and run the
r7rs#import. Otherwise, it
gauche#import. See Three forms of import, for the details.
An example of R7RS script:
(import (scheme base) (scheme write)) (display "Hello, world!\n")
If you’re already familiar with Gauche scripts, keep in mind that
R7RS program doesn’t treat
main procedure specially; it
just evaluates toplevel forms from top to bottom. So the following
script doesn’t output anything:
(import (scheme base) (scheme write)) (define (main args) (display "Hello, world!\n") 0)
To access the command-line arguments in R7RS scripts, use
(scheme process-context) library
(see R7RS process context, also see Command-line arguments).
gosh is invoked with
-r7 option and no script file is
given, it enters an R7RS REPL mode. For the convenience, the following
modules (“libraries”, in R7RS term) are pre-loaded.
(scheme base) (scheme case-lambda) (scheme char) (scheme complex) (scheme cxr) (scheme eval) (scheme file) (scheme inexact) (scheme lazy) (scheme load) (scheme process-context) (scheme read) (scheme repl) (scheme time) (scheme write)
Besides, the history variables
are available (See Working in REPL, for the details of history variables).
You can know you’re in R7RS REPL by looking at the prompt, where
gosh shows the current module (
To switch Gauche REPL from R7RS REPL, import
(gauche base) and
user module using
gosh[r7rs.user]> (import (gauche base)) #<undef> gosh[r7rs.user]> (select-module user) #<undef> gosh>
(select-module gauche) but that’s usually not what you
want to do—changing
gauche module can have unwanted side effects.)
When you’re working on R7RS code in file and load it into R7RS REPL
(for example, if you’re using Emacs Scheme mode, C-c C-l does the job),
make sure the file is in proper shape as R7RS; that is, the file
must start with appropriate
import declarations, or
the file contains
define-library form(s). If you load
file without those forms, it is loaded into Gauche’s
no matter what your REPL’s current module is,
and the definitions won’t be visible from
gosh enters Gauche REPL when no script file is given.
See Working in REPL, for detailed explanation of using REPL.
To switch Gauche REPL to R7RS REPL, simply use r7rs-style import;
user#import knows you want R7RS and make a switch.
gosh> (import (scheme base)) #<undef> gosh[r7rs.user]>
If you don’t start
-r7 option, however,
only the libraries you given to
user#import are loaded at
If you want to switch the “vanilla” r7rs environment, that is, even
(scheme base), then you can use
module and directly select
gosh> (use r7rs) gosh> (select-module r7rs.user) gosh[r7rs.user]>
If you do this, the only
bindings visible initially are
define is undefined!
You have to manually do
(import (scheme base)) etc. to start
writing Scheme in this environment.