Hello World in Common Lisp
Your first Common Lisp program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Common Lisp program and experience one of the most powerful and enduring programming languages ever created.
The Code
Create a file named hello.lisp:
| |
Understanding the Code
Let’s break down this simple but powerful expression:
(format t "Hello, World!~%")- In Common Lisp (and all Lisps), code is written as S-expressions using parentheses. The first element is the function name, followed by arguments.format- A powerful built-in function for formatted output, similar to C’sprintfbut more flexible.t- Represents “true” or standard output (*standard-output*). In Lisp,tis the canonical truth value."Hello, World!~%"- A format string where~%is the newline directive.
Why format Instead of print?
Common Lisp actually has multiple output functions:
| |
format is preferred because it provides the most control over output formatting and is the idiomatic choice in Common Lisp.
Running with Docker
The easiest way to run this without installing Common Lisp locally:
| |
Running Locally
If you have SBCL installed:
| |
Expected Output
Hello, World!
Understanding S-Expressions
Common Lisp uses prefix notation with parentheses, called S-expressions (symbolic expressions):
| |
The uniform syntax is what makes Lisp’s powerful macro system possible - code and data share the same structure.
Alternative Approaches
Using print Functions
| |
Defining a Main Function
For a more structured program:
| |
defun- Defines a functionmain- The function name (any name works; Common Lisp doesn’t require a specific entry point)()- Empty parameter list"A simple..."- Optional documentation string (docstring)(main)- Calls the function
Using format Directives
The format function is incredibly powerful:
| |
Common format directives:
~a- Aesthetic (human-readable)~s- Standard (machine-readable, with quotes for strings)~d- Decimal integer~f- Floating point~%- Newline~~- Literal tilde
Key Concepts
- S-Expressions - Code is written as nested lists:
(function arg1 arg2) - Prefix Notation - The operator/function always comes first
- Homoiconicity - Code and data have the same structure (lists)
- Dynamic Typing - Types are checked at runtime, not compile time
- REPL-Driven - Lisp is designed for interactive development
The REPL Experience
Common Lisp shines in the REPL (Read-Eval-Print Loop):
$ sbcl
This is SBCL 2.4.0, an implementation of ANSI Common Lisp.
* (format t "Hello, World!~%")
Hello, World!
NIL
* (+ 1 2 3)
6
* (defun greet (name) (format t "Hello, ~a!~%" name))
GREET
* (greet "Lisper")
Hello, Lisper!
NIL
The REPL returns two things:
- Any output from the expression
- The return value (NIL for
formatsince it returns nothing meaningful)
Common Lisp vs Other Languages
Java:
| |
Python:
| |
Clojure (Modern Lisp):
| |
Common Lisp:
| |
Notice how Common Lisp is concise like Python but shares the parenthesized syntax with Clojure.
Understanding NIL and T
In Common Lisp, two special values are fundamental:
NIL- Represents false, empty list, and “nothing”T- Represents true
In our Hello World:
tas the first argument toformatmeans “output to standard output”formatreturnsNILbecause its purpose is side effects (printing), not returning a value
Why Common Lisp?
Even in this simple example, we see Common Lisp’s character:
- Simplicity - Uniform syntax for everything
- Power -
formatalone is more powerful than most languages’ output facilities - Interactivity - REPL-driven development from the start
- Documentation - Docstrings are first-class citizens
- Flexibility - Multiple ways to accomplish the same task
SBCL (Steel Bank Common Lisp)
Our Docker image uses SBCL, which is:
- Fast - Compiles to native machine code
- Standard-compliant - Fully implements ANSI Common Lisp
- Actively maintained - Regular releases with improvements
- Open source - Derived from CMU Common Lisp
Other implementations exist (CCL, ECL, CLISP), but SBCL is the most popular for general development.
Next Steps
Continue exploring Common Lisp’s unique features:
- Variables and data types (symbols, lists, vectors)
- Functions and lambda expressions
- The powerful
formatfunction - List processing (car, cdr, cons)
- Macros - Lisp’s defining feature
- CLOS - The Common Lisp Object System
Common Lisp rewards patience. Its unusual syntax hides one of the most powerful and flexible programming environments ever created.
Welcome to the world of Lisp!
Running Today
All examples can be run using Docker:
docker pull clfoundation/sbcl:latest