Writing a library

Sagittarius provides 2 styles to write a library, one is R6RS style and other one is R7RS style. Both styles are processed the same and users can use it without losing code portability.

Following example is written in R6RS style, for the detail of library syntax please see the R6RS document described in bellow sections.

(library (foo)
  (export bar)
  (import (rnrs))

 (define bar 'bar) )

The library named (foo) must be saved the file named foo.scm, foo.ss, foo.sls or foo.sld (I use .scm for all examples) and located on the loading path, the value is returned by calling add-load-path with 0 length string.

Library file extensions

The file extension also controls the reader mode.

.ss and .sls

When a file with one of the extensions is loaded during library importing, then #!r6rs directive is applied to the target file by default.

.sld

When a file has this extensions, then #!r7rs directive is applied to the target file by default.

.scm

This file extension uses #!compatible directive.

If the loading file contains other directive, then the default directive is overwritten.

If you want to write portable code yet want to use Sagittarius specific functionality, then you can write implementation specific code separately using .sagittarius.scm, .sagittarius.ss, .sagittarius.sls or .sagittarius.sld extensions. This functionality is implemented almost all R6RS implementation. If you use R7RS style library syntax, then you can also use cond-expand to separate implementation specific functionalities.

Hidden library

If you don't want to share a library but only used in specific one, you can write both in one file and name the file you want to show. For example;

(library (not showing)
  ;; exports all internal use procedures
  (export ...)
  (import (rnrs))
;; write procedures
...
)

(library (shared)
  (export shared-procedure ...)
  (import (rnrs) (not showing))
;; write shared procedures here
)

Above script must be saved the file named shared.scm. The order of libraries are important. Top most dependency must be the first and next is second most, so on.

Note: This style can hide some private procedures however if you want to write portable code, some implementations do not allow you to write this style.