Racket Language and raco tools

Version 9.3 · source

Start here

racket
Start the REPL
racket <file.rkt>
Run a module
racket -e '<expr>'
Evaluate an expression
raco make <file.rkt>
Compile to bytecode
raco test <path>
Run tests
raco pkg install <pkg>
Install a package
raco exe -o <out> <file.rkt>
Build an executable
raco docs <term>
Search the docs

Concepts

  1. Write #lang racket in app.rkt
  2. Compile compiled/*.zo bytecode
  3. Test test submodule
  4. Ship executable + distribute

Run racket

racket
Start the REPL
racket <file.rkt> [<args>...]
Run a module with arguments
racket -e '<expr>'
Evaluate an expression and print it
racket -l racket/base -e '<expr>'
Eval with a fast-loading language
-e needs bindings in scope; -l racket/base is quicker to load than the default racket/init.
racket -i <file.rkt>
Run a file, then enter the REPL
racket -t <file.rkt>
Require a file and its main submodule
racket -l <lib>
Require a library collection path
racket -v
Print the version banner

The REPL

(exit) / Ctrl+D
Leave the REPL
,help
Show REPL help
,enter <file.rkt>
Enter a module's namespace
,require <module>
Require a module
,reload
Reload the last required module
,time <expr>
Time an expression

raco make and exe

raco make <file.rkt>
Compile a module and its dependencies
raco exe -o <out> <file.rkt>
Build a stand-alone executable
raco distribute <dir> <exe>
Package an exe with its runtime files
raco setup <collection>
Rebuild one collection

Tests

raco test <file.rkt>
Run a module's test submodules
raco test <dir>
Test all modules in a directory
raco test -c <collection>
Test an installed collection
raco test -p <package>
Test an installed package
raco test -j <n> <path>
Run tests in parallel
(module+ test ...)
Add tests inside the module
The test submodule is skipped when the module runs normally and only runs under raco test.
(require rackunit)
Load the unit-test library
(check-equal? <actual> <expected>)
Check two values are equal
(check-true <expr>)
Check an expression is true
(check-exn <pred> (lambda () ...))
Check that code raises an error
(test-case "<name>" ...)
Group checks under a name

Packages

raco pkg install <pkg>
Install a package from the catalog
raco pkg install --auto <pkg>
Install and fetch missing dependencies
raco pkg update <pkg>
Update a package
raco pkg update --all
Update all packages
raco pkg remove <pkg>
Uninstall a package
raco pkg show
List installed packages
raco pkg install --link <dir>
Link a local directory as a package
raco pkg install <git-url>
Install from a Git URL
raco pkg new <name>
Create stub files for a package
raco pkg catalog-show <pkg>
Look up a package in the catalogs

Tools and docs

raco docs <term>
Search the documentation
raco fmt <file.rkt>
Print the formatted program
Needs the fmt package: raco pkg install fmt.
raco fmt -i <file.rkt>
Format in place
raco expand <file.rkt>
Show macro-expanded code
raco help
List all raco commands

Language basics

#lang racket
Full language with common libraries
#lang racket/base
Small, fast-loading language
(require <module>)
Import a library or file
(require "<file.rkt>")
Import a sibling file by path
(provide <id> ...)
Export bindings from a module
(provide (all-defined-out))
Export everything defined here
(define (<f> <x>) ...)
Define a function
(module+ main ...)
Code run by racket <file>
(define args (current-command-line-arguments))
Read command-line arguments
(command-line #:args (<a>) ...)
Parse flags and arguments
(displayln <v>)
Print a value and newline
(for ([x (in-list <l>)]) ...)
Loop over a list

Recipes

Script with tests
#lang racket/base(provide add)(define (add a b) (+ a b))(module+ test  (require rackunit)  (check-equal? (add 1 2) 3))(module+ main  (displayln (add 1 2)))
Compile, test, ship
raco make main.rktraco test .raco exe -o app main.rktraco distribute dist app
Format and install tools
raco pkg install fmtraco fmt -i *.rkt
Quick one-liners
racket -e '(+ 1 2)'racket -l racket/base -l racket/list -e '(range 5)'racket -i app.rkt