RacketModulesLibrariesPackage-Management
Working with Modules and Libraries in Racket | Schema Programming Part 8
2.755 min read
Md Nasim Sheikh
As your code grows, putting everything in one file becomes unmanageable. Racket's module system helps you organize code into logical, reusable units.
Advertisement
Creating a Module
Every file starting with #lang racket is essentially a module. By default, definitions are private. You must explicitly provide them to make them public.
math-utils.rkt
#lang racket
(provide add-five
square)
(define (add-five x) (+ x 5))
(define (square x) (* x x))
(define (secret x) "I am hidden") ; Not exported
Using a Module (require)
To use the code from another file, use require.
main.rkt
#lang racket
(require "math-utils.rkt")
(display (add-five 10)) ; 15
; (secret 10) ; Error: unbound identifier
Renaming and prefixing
You can modify imports to avoid naming conflicts.
(require (prefix-in math: "math-utils.rkt"))
(math:square 4)
Advertisement
Installing Third-Party Packages
Racket has a central package repository: pkgs.racket-lang.org.
- Find a package (e.g.,
csv-reading). - Install it via command line:
raco pkg install csv-reading - Require it:
(require csv-reading)
Collections
Standard library modules are often organized into collections.
(require racket/math)
(require racket/list)
Summary
- Provide: Export functions.
- Require: Import functions.
- Raco: The Racket Command-line tool for package management.
In the next part, we'll talk to the outside world: Input/Output and File Handling.
Quick Quiz
Which keyword is used to make a function available to other modules?
Written by
Md Nasim Sheikh
Software Developer at softexForge
Verified Author150+ Projects
Published: