Racket Cheat Sheet
Fundamental Racket syntax covering definitions, higher-order functions, list operations, and pattern matching with match.
2 PagesIntermediateApr 10, 2026
Basics
Definitions and printing.
racket
#lang racket;; Definitions and printing(define name "World")(displayln (string-append "Hello, " name "!"))(define (square x) (* x x))(displayln (square 5))
Functions
Lambdas, local bindings, and higher-order functions.
racket
;; Anonymous functions (lambda)(define add (lambda (a b) (+ a b)))(displayln (add 2 3));; let for local bindings(let ([x 1] [y 2]) (displayln (+ x y)));; Higher-order functions(map (lambda (x) (* x x)) '(1 2 3 4))(filter even? '(1 2 3 4 5 6))(foldl + 0 '(1 2 3 4))
Lists
Core list operations.
- '(1 2 3)- Quoted list literal
- (cons 1 '(2 3))- Prepends an element, yields '(1 2 3)
- (car lst)- First element of a list
- (cdr lst)- Rest of the list after the first element
- (list 1 2 3)- Constructs a list from its arguments
- (append lst1 lst2)- Concatenates two lists
Pattern Matching
Destructuring with match.
racket
(define (describe x) (match x [(? number?) "a number"] [(list a b) (format "pair of ~a and ~a" a b)] ['() "empty list"] [_ "something else"]))(displayln (describe 5))(displayln (describe (list 1 2)))
Special Forms
Core syntactic forms.
- (define x 5)- Binds a value to a name
- (if cond then else)- Conditional expression
- (cond [test1 res1] [else default])- Multi-branch conditional
- (lambda (args) body)- Anonymous function
- (struct point (x y))- Defines a simple record type
- (let loop ([i 0]) (when (< i 5) (loop (+ i 1))))- Named let for recursion/looping
Structs
Define compound data with struct.
racket
(struct point (x y) #:transparent)(define p (point 3 4))(point-x p) ; => 3(point? p) ; => #t; functional update(struct-copy point p [x 10]); mutable fields(struct counter (n) #:mutable)(define c (counter 0))(set-counter-n! c 5)
Higher-Order Functions
map, filter, foldl and lambda composition.
racket
(map (lambda (x) (* x x)) '(1 2 3)) ; '(1 4 9)(filter even? '(1 2 3 4)) ; '(2 4)(foldl + 0 '(1 2 3 4)) ; 10(foldr cons '() '(1 2 3)) ; '(1 2 3); curry & compose(define add1 (curry + 1))(define inc-then-double (compose (curry * 2) add1))(inc-then-double 3) ; 8
Hygienic Macros
Syntax transformations with define-syntax-rule.
racket
(define-syntax-rule (swap! a b) (let ([tmp a]) (set! a b) (set! b tmp)))(define-syntax my-unless (syntax-rules () [(_ cond body ...) (if cond (void) (begin body ...))]))(my-unless #f (displayln "runs"))
Contracts & Modules
Runtime contracts and module system.
- (provide (contract-out [f (-> number? number?)]))- attach a contract to an exported function
- (-> a? b?)- function contract: takes a?, returns b?
- (->* (req) (opt) res)- contract with required and optional args
- (require racket/list)- import a module's provided bindings
- (provide (all-defined-out))- export every top-level definition
- #lang racket/base- minimal language variant for faster startup
Hash Tables
Immutable and mutable dictionary operations.
- (hash 'a 1 'b 2)- create an immutable hash table
- (make-hash)- create a mutable hash table
- (hash-ref h 'a default)- look up a key with an optional default
- (hash-set h 'c 3)- functional update returning a new hash
- (hash-set! h 'c 3)- in-place update of a mutable hash
- (hash-keys h) / (hash-values h)- list of keys or values
Pro Tip
Prefer match over nested cond/if for destructuring lists and structs — it makes intent explicit and catches unhandled cases more readably.
Was this cheat sheet helpful?
Explore Topics
#Racket#RacketCheatSheet#Programming#Intermediate#Functions#Lists#PatternMatching#SpecialForms#DataStructures#CheatSheet#SkillVeris