A Friendly Introduction to Racket
A Welcoming Guide to Racket
"Lisp is worth learning for the profound enlightenment experience you will have when you finally get it." — Eric S.
Today, we are diving into one of the most ancient and eccentric lineages of programming. Imagine a world where code is treated as data, where parentheses define the very architecture of the program, and where software possesses the ability to author other software.
By the conclusion of this guide, you will have the tools to construct your own custom syntax.
A Glimpse into the Past
Lisp was conceived in 1958 by John McCarthy at MIT. To put its age into perspective, it is the second-oldest high-level language still in active use—surpassed only by Fortran (1957).
Because Lisp arrived so early, it pioneered concepts that we now take for granted as "modern" staples:
- Garbage Collection: First invented specifically for Lisp.
- First-Class Functions: The ability to treat functions as arguments.
- The REPL: The Read-Eval-Print Loop (the interactive shell used by Node, Python, and Julia) originated here.
- Homoiconicity: A property where the code is structured as a data structure native to the language itself.
For a long time, Lisp was the primary engine of Artificial Intelligence. During the 70s and 80s, companies like Symbolics and LMI even built standard computers Lisp Machines—hardware designed specifically to execute Lisp. However, the "AI winter" caused funding to vanish, and Lisp shifted from a mainstream powerhouse to a specialized "cult" language. Yet, the beauty of Lisps is that they don't die; they mutate.
The Evolution: Lisp Scheme Racket
In 1975, Guy Steele and Gerald Sussman developed Scheme, a version of Lisp characterized by its mathematical elegance and minimalism. It became the gold standard for computer science education, most notably in the seminal text Structure and Interpretation of Computer Programs (SICP).
Later, in 1995, Matthias Felleisen's team introduced PLT Scheme, tailored for research and teaching. By 2010, this evolved into Racket. Racket is no longer just a dialect of Scheme; it is a language for creating languages. This philosophy is known as language-oriented programming: if a specific problem requires a unique language to solve efficiently, Racket allows you to build that language in a single afternoon.
Historical Milestones
| Year | Event |
|---|---|
| 1958 | John McCarthy creates Lisp at MIT |
| 1975 | Scheme is developed by Sussman & Steele |
| 1984 | Common Lisp becomes a standard |
| 1995 | PLT Scheme (the precursor to Racket) is launched |
| 2007 | Clojure emerges (Lisp for the JVM) |
| 2010 | PLT Scheme is officially renamed Racket |
| Today | You are reading this and preparing to use parentheses! UwU |
Who is still using Lisp?
The Lisp family is more prevalent than it seems:
- Clojure: Used in high-scale production by startups and banks (e.g., Nubank).
- Common Lisp: Utilized in scientific computing and flight planning (e.g., ITA Software/Google Flights) via the SBCL compiler.
- Emacs Lisp: Millions of users interact with Lisp daily via the Emacs editor.
- Guile/Guix: A Linux distribution where the entire configuration is written in Scheme.
- Racket: Used for formal verification (
Rosette), publishing (Pollen), and academic research.
Additionally, new variants continue to emerge, such as Fennel (compiles to Lua), Janet, and Hy (Lisp syntax for Python).
Pop Culture Easter Egg: In The Amazing Digital Circus (Episode 8), Kinger attempts to reset Caine using a terminal. You can spot that Caine—an AI created in 1996—is programmed in Lisp!
Getting Started (5-Minute Setup)
- Visit racket-lang.org.
- Install the version appropriate for your OS (Windows, macOS, or Linux).
- Open DrRacket. You will see two main sections:
- Top Area: Where you write your permanent definitions.
- Bottom Area: The REPL for instant testing.
At the very top of your definitions area, you must include:
#lang racket
This directive tells the system which language to use, as Racket is essentially a factory for languages. If you prefer the command line, use racket for the REPL and raco for package management.
First Contact: The Expression
In the REPL, try these:
( + 1 2 ) ; Result: 3
( * 3 ( + 2 2 )) ; Result: 12
( string-append "hello" " " "world" ) ; Result: "hello world"
The fundamental logic of Lisp can be summarized by this LaTeX formula:
There is no complex operator precedence to memorize. The parentheses, while daunting at first, actually represent a lack of arbitrary rules.
Definitions and Logic
#lang racket
( define pi-approx 3.14159 )
( define ( circle-area r )
( * pi-approx r r ))
( circle-area 2 ) ; Returns 12.56636
definewith a single name creates a constant.definewith a list creates a function.- Anonymous functions use
lambda(derived from Church's lambda calculus):
( lambda ( x ) ( * x x )) ; A nameless function
(( lambda ( x ) ( * x x )) 5 ) ; Applied to 5, returns 25
Lists: The Core of Lisp
Lisp is an acronym for LISt Processing. Lists are the primary building block:
( list 1 2 3 ) ; Result: '(1 2 3)
( ' ( 1 2 3 ) ) ; Same result, using the quote mark
( first ' ( 1 2 3 )) ; Result: 1
( rest ' ( 1 2 3 )) ; Result: '(2 3)
( cons 0 ' ( 1 2 3 )) ; Result: '(0 1 2 3)
( length ' ( a b c )) ; Result: 3
The quote mark ' tells Racket to treat the list as data rather than as code to be executed.
Higher-Order Functions
Passing functions into other functions is seamless in Racket:
| Function | Purpose | Example |
|---|---|---|
map | Transforms every element | (map (lambda (x) (* x x)) '(1 2 3)) '(1 4 9) |
filter | Selects elements based on a predicate | (filter even? '(1 2 3 4)) '(2 4) |
foldl | Accumulates a list into a single value | (foldl + 0 '(1 2 3 4 5)) 15 |
Recursion: Thinking in Spirals
In Lisp, you replace "looping" with recursion. Instead of "repeat times," you ask: "What is my base case, and how do I move toward it?"
( define ( factorial n )
( if ( = n 0 )
1
( * n ( factorial ( - n 1 )))))
( factorial 5 ) ; Result: 120
To wrap up, Racket also includes powerful built-in graphics libraries for visual programming.
Visual Gallery
