Node:Intro to Writing New Modules, Next:, Previous:Intro to Using Guile Modules, Up:Guile Modules



5.5.2 Intro to Writing New Modules

Of course it is possible to write modules yourself. Using modules for structuring your programs makes them more readable and lets you distribute them more easily. Also, explicitly defining the procedures and variables which are exported from a module adds documentation to the source and specifies the interface a module provides.

In Guile, you can create new modules and switch to existing modules in order to add bindings to them using the syntactic form define-module.

(define-module (foo bar))

(define (frob x) x)

Will create the module (foo bar).1 All definitions following this statement will add bindings to the module (foo bar), and these bindings will not be visible outside of the module. To make the bindings accessible to other modules, you have to export them explicitly using one of the following means:

After exporting, other modules can access the exported items simply by using use-modules to load the module (foo bar).


Footnotes

  1. It is only convention that the module names in this section have two elements. One or more than two elements are perfectly fine, such as (foo) or (foo bar braz)