20. Libraries

How to make a library of functions that you can use in other files.

Use require and provide to make functions from one file available in another file.

Require and provide

Require loads functions from another file. Provide makes functions available for other files to load.

Provide

The provide command makes functions available for other files to use. You put it into the file with the functions you want to make available.

If you just want to make every function in the file available, there is a shortcut to do that:

(provide (all-defined-out))

Sometimes you want to only make some of your functions available. Normally you would not want to provide your helper functions, for example.

(provide posn-add posn-sub posn-scale posn-distance)

Require

In order to use the functions in another file, you need to require that file. You already know (require picturing-programs) but when we use require we need to give the whole filename.

(require "posn-util.rkt")

Checklist to make this work:

  1. The posn-util file has a provide statement inside it.
  2. The file with the require is saved and in the same folder as the posn-util file.
Last modified January 23, 2024: Reordered and clarified. (93788ca)