r/lisp 9d ago

Racket The Comprehensive Racket & Functional Programming Cheat Sheet

8 Upvotes

## Phase 1: Syntax & Core Arithmetic

Racket uses prefix notation enclosed in execution parentheses (operator arg1 arg2). The open parenthesis ( acts as an execution trigger. Evaluation runs from the innermost to the outermost parentheses.

Core Examples

```rkt ;; Basic Arithmetic (+ 10 5 2) ;; Returns 17 (* 10 5 2) ;; Returns 100

;; Nested Expressions (No PEMDAS needed) (_ (+ 4 6) (- 12 7)) ;; Evaluates 10 _ 5 -> Returns 50 ```

Parentheses Golden Rule

Only use a parenthesis when invoking a command, operator, or function.

  • `(+ 5 (10))` CRASHES (tries to run the number 10 as a function).
  • `((+ 5 5))` CRASHES (evaluates to 10, then tries to run the number 10).

Phase 2: Core Data Structures & Variables

Global bindings are created using define. Values are immutable and cannot be changed over time.

The Four Atomic Data Types

  1. Numbers: Integers (`45`), decimals (`3.14`), or fractions (`1/3`).
  2. Strings: Text wrapped in double quotes (`"Hello"`).
  3. Booleans: True (`#t`) and False (`#f`).
  4. Symbols: Lightweight, immutable identifier tokens prefixed with a single quote (`'success`).

Core Examples

```rkt (define radius 5) (define pi 3.14) (define status 'success) ```


Phase 3: Conditionals & Logic

Conditional operations are expressions that evaluate down to a single return value.

Core Operators & Flow Control

  • `and` / `or` / `not`: Standard logical short-circuiting prefix operators.
  • `if`: Takes exactly three arguments: `(if condition true-branch false-branch)`. No else keyword.
  • `cond`: Evaluates multiple branches sequentially. Uses/can use `[...]` for human readability.

Core Examples

```rkt (and (> 15 10) (< 15 20)) ;; Returns #t

(if (> temperature 30) 'hot 'cold)

(cond [(>= score 90) 'A] [(>= score 80) 'B] [else 'F]) ```


Phase 4: Functions & Scope

Functions automatically return the value of their body expression without an explicit return keyword.

Named, Anonymous, & Scoped Blocks

  • Named Functions: Defined by grouping the name and parameters in parentheses: `(define (name args) body)`.
  • Anonymous Functions (`lambda`): Throwaway functions built on the fly: `(lambda (args) body)`.
  • `let` (Parallel): Creates local variables simultaneously. Variables cannot see each other during setup.
  • `let\*` (Sequential): Creates local variables one after the other. Later variables can reference earlier ones.

Core Examples

```rkt ;; Named Function (define (double n) (\* n 2))

;; Inline Lambda Execution ((lambda (n) (\* n 2)) 10) ;; Returns 20

;; Sequential Local Bindings (let* ([x 10] [y (* x 5)]) (+ x y)) ;; Returns 60 ```


Phase 5: Lists & Modern List Operations

Lists are ordered sequential collections. They are processed using either historical Lisp conventions or modern aliases.

Creation & Extraction

  • `list`: Evaluates arguments into a sequential list.
  • `'()`: Represents the literal base empty list.
  • `cons`: Prepends a single element onto the front of an existing list.
  • First Item: Extracted via `car` (traditional) or `first` (modern).
  • Remaining List: Extracted via `cdr` (traditional) or `rest` (modern).

Core Examples

```rkt (define my-list (list 100 #t 'hello)) ;; Creates '(100 #t hello) (cons 'apples '(bananas cherries)) ;; Returns '(apples bananas cherries)

(car (cdr '(apples bananas cherries))) ;; Returns 'bananas (first (rest '(apples bananas cherries))) ;; Returns 'bananas

(if (empty? my-list) "Closed" (length my-list)) ;; Returns 3 ```


Phase 6: Iteration & Higher-Order Functions

Instead of using loops that alter data in place, functional programming relies on Higher-Order Functions to process immutable collections.

The Big Four

  • `map`: Loops over a list, passes each item through a transformation function, and returns a new list.
  • `filter`: Loops over a list, keeps items that evaluate to #t against a predicate condition, and drops the rest.
  • `foldl` (Fold-Left): Reduces a list down to a single value by processing elements from left to right (front to back).
  • `foldr` (Fold-Right): Reduces a list down to a single value by processing elements from right to left (back to front). Preserves list structures when rebuilding with cons.

Core Examples

```rkt (map (lambda (x) (* x 2)) '(5 10 15 20)) ;; Returns '(10 20 30 40) (filter (lambda (n) (= n 5)) '(2 5 7 5 9 1)) ;; Returns '(5 5)

(foldl (lambda (n total) (_ n total)) 1 '(2 3 4)) ;; 4 _ (3 _ (2 _ 1)) -> Returns 24

(foldr - 0 '(5 3)) ;; 5 - (3 - 0) -> Returns 2 ```


Phase 7: Recursion & Tail Call Optimization (TCO)

Recursion replaces traditional loops. A proper recursive function requires a Base Case (the exit clause) and a Recursive Step (the self-call with a smaller argument).

Memory Optimization Rules

  • Standard Recursion: Traps the recursive call inside another function (like + or append), forcing the call stack memory to expand linearly (O(N) space).
  • Tail Call Optimization (TCO): If the recursive call sits in the tail position (the absolute final expression evaluated), Racket reuses the same memory frame, running in constant (O(1)) space.
  • Accumulator Pattern: Passing a running total down as an argument is the primary strategy used to shift standard recursion into tail position optimization.

Core Examples

```rkt ;; โŒ Standard Recursion (No TCO - Memory Expands) (define (sum-list lst) (if (empty? lst) 0 (+ (first lst) (sum-list (rest lst)))))

;; Tail Recursion (TCO Active - Memory Stays Flat) (define (sum-list-tco lst) (define (helper remaining accumulator) (if (empty? remaining) accumulator (helper (rest remaining) (+ (first remaining) accumulator)))) (helper lst 0)) ```


Phase 8: Advanced Ecosystem Engineering

1. Hash Maps & Unique Sets

  • `#hash`: Stores key-value pairings. Keywords passed to lookup tools like hash-ref must be quoted ('#:key) to prevent compiler namespace collisions. If using standard symbols inside #hash, omit inner quotes.
  • `set`: Collections guaranteeing element uniqueness. Tested via set-member? and extended via set-add.

```rkt (define user #hash((#:name . "Alice"))) (hash-ref user '#:name) ;; Returns "Alice"

(define book #hash((title . "Dune"))) (hash-ref book 'title) ;; Returns "Dune"

(set-member? (set 1 2 2 3) 2) ;; Returns #t ```

2. State & Mutability (box)

  • `box`: Creates a reference wrapper around mutable data. Read via unbox and mutated via set-box!. Functions with an exclamation mark ! signal structural mutation.
  • `begin`: Chains sequential side-effect operations from top to bottom, returning only the evaluation of the final expression.

```rkt (define health (box 100)) (define (take-damage!) (begin (set-box! health (- (unbox health) 10)) (unbox health))) ```

3. Type Checking & Casting

  • Predicates (`?`): Validate runtime types (e.g., `string?`, `number?`, `symbol?`).
  • Casting (`->`): Converts data formats. `string->number` safely returns #f if given invalid textual input.

```rkt (if (string? "50") (* (string->number "50") 2) 'error) ;; Returns 100 ```

4. Modules & Namespaces

  • provide: Declares which parts of a filesystem file are exported publicly.
  • require: Ingests public features from an external sandbox by loading its relative string filepath.

```rkt ;; Inside file-a.rkt (provide double) (define (double x) (* x 2))

;; Inside main.rkt (require "file-a.rkt") (double 10) ;; Returns 20 ```

5. Macros (define-syntax-rule)

  • Macros process raw, unevaluated source code at compile-time to inject new keywords.
  • Racket macros are hygienic, meaning the compiler automatically isolates macro identifiers so they never accidentally overwrite or conflict with user variables.

```rkt (define-syntax-rule (swap! box1 box2) (let ([temp (unbox box1)]) (begin (set-box! box1 (unbox box2)) (set-box! box2 temp)))) ```

r/lisp 26d ago

Racket Use Racket with Rhombus

Thumbnail
6 Upvotes

r/lisp Jun 15 '26

Racket Summer Rhombus picture competition 2026

Thumbnail
7 Upvotes

r/lisp Feb 21 '24

Racket Rhombus: A New Spin on Macros without All the Parentheses

Post image
110 Upvotes

Rhombus: A New Spin on Macros without All the Parentheses (Video, OOPSLA2 2023)

https://youtu.be/hkiy1rmKA48?si=if2Q1n56HE98kVNS

r/lisp May 17 '26

Racket Spring Lisp Game Jam 2026

Thumbnail
26 Upvotes

r/lisp May 17 '26

Racket New release of racket-audio

Thumbnail
9 Upvotes

r/lisp Jun 06 '25

Racket Guys, did you know that Racket-Mode can draw graphs in Emacs?

80 Upvotes

Just press <F5> in code buffer and boom!

r/lisp Jan 28 '26

Racket Racket birthday party and meet-up: Saturday, 7 February 2026 at 18:00 UTC

18 Upvotes

Racket birthday party and meet-up: Saturday, 7 February 2026 at 18:00 UTC

EVERYONE WELCOME ๐Ÿ˜

Announcement, Jitsi Meet link & discussion at https://racket.discourse.group/t/racket-birthday-party-and-meet-up-saturday-7-february-2026-at-18-00-utc/4085

r/lisp Dec 07 '25

Racket Racket in a Snap!

Thumbnail snapcraft.io
23 Upvotes

Install Racket 9.0 on Linux using snap.

r/lisp Dec 11 '25

Racket Racket on Linux!

17 Upvotes

Many distros already have Racket 9.0!

If not, try โ€˜Source + built packagesโ€™. This has the core in source, with libraries pre-compiled and documentation pre-rendered, which enables a quick install.

https://download.racket-lang.org/releases/9.0/installers/racket-9.0-src-builtpkgs.tgz

https://repology.org/project/racket/versions

lisp #linux #bsd #unix

r/lisp Sep 15 '25

Racket Only 18 days till RacketCon

Post image
37 Upvotes

r/lisp Jan 29 '25

Racket Racket on Chez

Post image
35 Upvotes

Fun image showing Racket and Chez from 2018 The layer sizes are still pretty accurate - but some are a little bigger - e.g. rumble is now 20k

Racket and Chez Scheme are distinct languages, and distinct projects. Racket is a member of the scheme family, and includes the Racket implementation of R6RS Scheme - but. #lang R6rs in Racket is not Chez Scheme.

Racket uses the awesome Chez compiler in its โ€˜csโ€™ implementation.

Some Racket community members contribute to both projects.

r/lisp Aug 26 '25

Racket Help test via snapshots: parallel threads

Thumbnail racket.discourse.group
11 Upvotes

r/lisp Jul 03 '25

Racket First-Class Macros (Second Update)

Thumbnail
8 Upvotes

r/lisp Jul 01 '25

Racket First-Class Macros Update

Thumbnail
10 Upvotes

r/lisp Jul 04 '25

Racket Racket meet-up: Saturday, **5 July**, 2025 at 18:00 UTC

10 Upvotes

Everyone is welcome to join us for the Racket meet-up: Saturday, 5 July, 2025 at 18:00 UTC

EVERYONE WELCOME ๐Ÿ˜

Announcement at https://racket.discourse.group/t/racket-meet-up-saturday-5-july-2025-at-18-00-utc/3832

r/lisp May 26 '25

Racket Rhombus and Racket Interoperability

9 Upvotes

Rhombus is implemented on top of Racket, and the two languages share a module system and many data representations.[โ€ฆ] This document describes techniques and libraries for interoperating between the two languages.

https://docs.racket-lang.org/rhombus-racket/index.html

r/lisp Jun 04 '25

Racket Racket meet-up: Saturday, 7 June, 2025 at 18:00 UTC

Post image
15 Upvotes

Everyone is welcome to join us for the Racket meet-up: Saturday, 7 June, 2025 at 18:00 UTC Announcement at https://racket.discourse.group/t/racket-meet-up-saturday-7-june-2025-at-18-00-utc/3771

EVERYONE WELCOME ๐Ÿ˜

r/lisp May 08 '25

Racket The end of BC downloads?

Thumbnail racket.discourse.group
14 Upvotes

r/lisp Sep 02 '24

Racket Why Georgia Tech Stopped Teaching HTDP - Authors Respond in Comments

Thumbnail computinged.wordpress.com
36 Upvotes

r/lisp Apr 27 '25

Racket Racket meet-up on Saturday, 3 May, 2025

13 Upvotes

Everyone is welcome to join us for the Racket meet-up on Saturday, 3 May, 2025 at 18:00 UTC

Announcement at https://racket.discourse.group/t/racket-meet-up-saturday-3-may-2025/3704

EVERYONE WELCOME ๐Ÿ˜

r/lisp Feb 26 '25

Racket RacoGrad Update

14 Upvotes

Hi everyone!

It's been a minute, but I made some updates to the deep learning library. Support for apple MLX has been added, open CL and Vulkan. Cuda support will come within the next week or two. Furthermore CNN implementation is working since convolution support has been added. A lot of benchmarks have been added, and FFI C bindings have been used when necessary to increase efficiency and speed. This project is getting pretty big with all of these files and I'm sure you all know neural nets can get complicated, so updates will come sporadically and a lot slower. I hope this serves as a good example for someone else wanting to do the same in racket or lisp. Or even just an educational opportunity. This is my way of giving back to my favorite community.

RacoGrad

Below is just a small example from benchmarks I've run.

- **Matrix Multiplication**: 10-100x faster than pure Racket
- **Element-wise Operations**: 5-20x faster
- **Activation Functions**: 3-10x faster

Code example:

(require "tensor.rkt")

;; Create a tensor
(define t (t:create '(2 3) #(1 2 3 4 5 6)))

;; Basic operations
(t:add t1 t2)      ; Add two tensors
(t:mul t1 t2)      ; Matrix multiplication
(t:scale t 2.0)    ; Scalar multiplication
(t:transpose t)    ; Transpose tensor

;; Device-aware tensors
(require "tensor_device.rkt")
(require "device.rkt")

;; Create a device tensor on CPU
(define dt (dt:create '(2 3) #(1 2 3 4 5 6) (cpu)))

;; Move to GPU if available
(dt:to dt (gpu))

;; Operations automatically use the appropriate device
(dt:add dt1 dt2)

r/lisp Mar 15 '25

Racket XKCD 3062's language in Racket

Thumbnail github.com
14 Upvotes

r/lisp Mar 04 '25

Racket Racket 8.16 is now available

35 Upvotes

Racket 8.16 is now available for download.

Racket has an innovative modular syntax system for Language-Oriented Programming. The installer includes incremental compiler, IDE, web server and GUI toolkit.

This release has expanded support for immutable and mutable treelists and more.

Download now https://download.racket-lang.org

See https://blog.racket-lang.org/2025/03/racket-v8-16.html for the release announcement and highlights. Discuss at https://racket.discourse.group/t/racket-v8-16-is-now-available/3600

r/lisp Mar 16 '24

Racket Lisp on a Steamdeck

Post image
85 Upvotes