1 Welcome to PLT Scheme
2 Scheme Essentials
3 Built-In Datatypes
4 Expressions and Definitions
5 Programmer-Defined Datatypes
6 Modules
7 Contracts
8 Input and Output
9 Regular Expressions
10 Exceptions and Control
11 Iterations and Comprehensions
12 Pattern Matching
13 Classes and Objects
14 Units (Components)
15 Reflection and Dynamic Evaluation
16 Macros
17 Performance
18 Running and Creating Executables
19 Compilation and Configuration
20 More Libraries
Bibliography
Index
Version: 4.0.2

 

6.1 Module Basics

The space of module names is distinct from the space of normal Scheme definitions. Indeed, since modules typically reside in files, the space of module names is explicitly tied to the filesystem at run time. For example, if the file "/home/molly/cake.ss" contains

  #lang scheme

  

  (provide print-cake)

  

  ; draws a cake with n candles

  (define (print-cake n)

    (printf "   ~a  \n" (make-string n #\.))

    (printf " .-~a-.\n" (make-string n #\|))

    (printf " | ~a |\n" (make-string n #\space))

    (printf "---~a---\n" (make-string n #\-)))

then it can be used as the source of a module whose full name is based on the path "/home/molly/cake.ss". The provide line exports the definition print-cake so that it can be used outside the module.

Instead of using its full path, a module is more likely to be referenced by a relative path. For example, a file "/home/molly/random-cake.ss" could use the "cake.ss" module like this:

  #lang scheme

  

  (require "cake.ss")

  

  (print-cake (random 30))

The relative reference "cake.ss" in the import (require "cake.ss") works because the "cake.ss" module source is in the same directory as the "random-cake.ss" file. (Unix-style relative paths are used for relative module references on all platforms, much like relative URLs.)

Library modules that are distributed with PLT Scheme are usually referenced through an unquoted, suffixless path. The path is relative to the library installation directory, which contains directories for individual library collections. The module below refers to the "date.ss" library that is part of the "scheme" collection.

  #lang scheme

  

  (require scheme/date)

  

  (printf "Today is ~s\n"

          (date->string (seconds->date (current-seconds))))

In addition to the main collection directory, which contains all collections that are part of the installation, collections can also be installed in a user-specific location. Finally, additional collection directories can be specified n configuration files or through the PLTCOLLECTS search path. Try running the following program to find out where your collections are:

  #lang scheme

  

  (require setup/dirs)

  

  (find-collects-dir) ; main collection directory

  (find-user-collects-dir) ; user-specific collection directory

  (get-collects-search-dirs) ; complete search path

We discuss more forms of module reference later in Module Paths.