Introduction

This book contains small, complete Common Lisp programs. Each chapter covers one self-contained .lisp file (most well under 100 lines) that you can read in a few minutes and run with sbcl --script. Nothing is left out: each chapter lists the whole program, explains how it works, shows a run, and suggests ways to use and extend it.

The examples are practical. They call large language models (Apple’s on-device model, Google Gemini, local Ollama models), query web search APIs and SPARQL knowledge graphs, scrape and render web pages, automate macOS apps, and implement classic algorithms from scratch: Bayesian inference, BM25 document ranking, a Hopfield network, a triple store, a vector store, an extractive summarizer, symbolic polynomial calculus, and reverse-mode automatic differentiation.

A note on style: the network examples run curl (via uiop:run-program) instead of loading an HTTP client library. This keeps dependencies few and makes each program easy to copy into your own projects.

What you need

  • SBCL: every program except the AppleScript library runs with sbcl --script filename.lisp.
  • Quicklisp: the Gemini, Ollama, search, SPARQL, and web-scraping examples need it.
  • curl: every network example uses it as the HTTP client.
  • API keys in environment variables: GOOGLE_API_KEY for Gemini, EXA_SEARCH_API_KEY and BRAVE_SEARCH_API_KEY for the web search chapter.
  • Ollama: a local Ollama server for the Ollama chapter.
  • Lightpanda: the Lightpanda headless browser binary.
  • macOS: for the AppleScript automation and Apple Foundation Model chapters.

Each program starts with a comment that shows how to run it. The chapters are independent, so read them in any order. To load every definition into one REPL session and experiment, see the last chapter on load.lisp. I load load.lisp from my SBCL .sbclrc file, so every function in this book, plus the help function that lists them, is ready in all my REPL sessions.

Which chapters run as-is

Chapter Needs beyond SBCL Output in this book
Apple Foundation Models macOS + Apple Intelligence + fm Real transcript
Gemini Quicklisp + GOOGLE_API_KEY Real transcript
Ollama Local ollama serve + pulled model Real transcript
Web search (Exa, Brave) Quicklisp + two API keys Real transcript, abridged
SPARQL Quicklisp Real transcript
Lightpanda lightpanda binary Real transcript, abridged
Web scraping Quicklisp Real transcript, abridged
AppleScript automation macOS (+ permissions per function) One real line; rest are live-state calls
Bayes, BM25, Hopfield, triple store, vector store, summarizer, symbolic math, autograd Nothing Real transcript, verbatim

All example files are in the book’s GitHub repository: https://github.com/mark-watson/Little-Bits-of-Common-Lisp_book.