Working with Libraries¶
Importing Standard Libraries¶
Every R7RS program starts by importing what it needs:
Multiple imports can be combined:
Import Modifiers¶
;; Import only specific names
(import (only (scheme base) map filter))
;; Import everything except certain names
(import (except (scheme base) error))
;; Rename on import
(import (rename (scheme base) (map scheme-map)))
;; Add a prefix to all imported names
(import (prefix (scheme char) char:))
(char:char-alphabetic? #\A) ;=> #t
Available Libraries¶
Kaappi includes all 14 R7RS standard libraries, 9 built-in SRFIs, and 42 portable SRFIs. See the Library Reference for the complete list.
Writing Your Own Library¶
Create a file mylib/math.sld:
(define-library (mylib math)
(export square cube factorial)
(import (scheme base))
(begin
(define (square x) (* x x))
(define (cube x) (* x x x))
(define (factorial n)
(let loop ((i n) (acc 1))
(if (= i 0) acc
(loop (- i 1) (* i acc)))))))
Use it from another file:
Library Search Paths¶
Kaappi searches for .sld files in this order:
- The current directory (
./) - The
./lib/subdirectory - Directories specified with
--lib-path
The library name (mylib math) maps to the file path mylib/math.sld.
See the Library Reference for export specs, import
modifiers, bytecode caching, cond-expand, and the complete library list.
Next: Advanced Features