A cheat sheet for an experienced R7RS Scheme developer to quickly get started with Racket! Here's a concise guide covering the key differences and additions:
Similarities
Racket is largely Scheme-compatible; most R7RS code will "just work"
Same syntax for define, lambda, let, cond, quote, quasiquote, etc.
Standard libraries like (scheme base), (scheme case-lambda), etc. are available (but not always necessary)
Differences & Additions
1. Module system
* Racket's module system is more powerful and explicit:
+ (module name lang ...) instead of R7RS's (define-library (name ...))
+ Lang can be racket (default), racket/base, scheme, or r7rs (for R7RS compatibility)
* Use (require racket/base) or (require scheme) to import standard libs
* No export, use provide instead: (provide f1 f2 ...)
Example:
racket
#lang racket/base
(define (add x y) (+ x y))
(provide add)
2. #lang directive
* Every Racket file starts with #lang <language> (like #!r6rs in R6RS)
* Common langs: racket, racket/base, typed/racket, scribble, r7rs
* Sets the language, module, and reader (more on this later)
3. Contracts (optional but awesome)
* Enforce function pre/post conditions and type-like constraints:
+ (define/contract (f x) (-> integer? integer?) (+ x 1))
* Use provide with contracts: (provide (contract-out [f (-> integer? integer?)]))
4. Structs (better than R7RS records)
* Define structs with: (struct point (x y) #:transparent)
+ #:transparent for auto-generated write method
* Accessors: point-x, point-y; mutators: set-point-x!, etc.
* Pattern matching with match: (match p [(point x y) (+ x y)])
5. Pattern matching
* (match exp [pattern body] ...) is more powerful than cond and case
* Supports lists, structs, regexps, and more:
+ (match lst [(list a b c) (list c b a)])
6. Immutable by default
* Lists, structs, and most data are immutable (use mcons for mutable cons)
* set! only works on mutable bindings (created with define-values)
7. Error handling
* Use raise instead of error (but error works too)
* Catch errors with with-handlers:
+ (with-handlers ([exn:fail? (λ (e) (displayln "oops"))]) (raise "fail"))
8. Macros
* define-syntax-rule (simpler than syntax-rules):
+ (define-syntax-rule (unless cond body ...) (if (not cond) (begin body ...)))
* syntax-parse (more powerful than syntax-case):
+ (define-syntax (m stx) (syntax-parse stx [(_ x) #'(add1 x)]))
9. Reader & S-exp extensions
* #reader directive for custom readers (e.g., #reader scribble/reader)
* racket/read for advanced S-exp reading
Porting R7RS code
Add #lang r7rs at the top (or (require r7rs) with #lang racket/base)
Replace (define-library ...) with (module ... r7rs)
Convert export to provide
Fix set-cdr!, set-car! to use mutable cons (mcons, mcar, mcdr)
Review letrec* and define-values usage (subtly different in Racket)
Common Racket libs to require
racket/base (default, minimal lib)
racket/list (extra list utils)
racket/string (string utilities)
racket/function (curry, const, etc.)
racket/port (string ports, etc.)
racket/match (pattern matching)
REPL & Tools
Use racket (CLI) or DrRacket (IDE) for interactive development
,enter (in REPL) to enter a module/file
raco command-line tool for pkg management, testing, and more