Language basics
27 steps in 9 sets of Lisp.
Common Lisp is the language where the program and its data share one shape: everything is an expression, parentheses are the syntax tree written out loud, and the REPL has been the center of the workflow since before most languages existed. The dialect here is portable ANSI Common Lisp as SBCL compiles it — defun and let, the list operations that gave the language its name, CLOS generic functions, and the macros that let the language extend itself.
This tour covers the working core: bindings and functions, cons cells and quoting, cond and recursion, the higher-order trio of mapcar/reduce/remove-if, hash tables and association lists, structures and CLOS, format's little language, and finally defmacro with backquote. The encore ships complete programs. Every seed compiles under SBCL with the full-warning gate, and the encore runs start to finish.
Values & bindings
- defparameter & defvarTwo ways to make a global:
defparameteroverwrites,defvarbacks off. - let & let*
letbinds all its names at once;let*builds each one on the last. - Strings & numbersJoining strings and dividing numbers, where Lisp keeps exact answers by default.
Lists & conses
- Cons cellsEvery Lisp list is really a chain of two-slot cons cells.
- Building listsGrowing a playlist with
push, then straightening it out withreverseandappend. - QuotingA quote is the difference between running
(+ 2 3)and holding on to it.
Control flow
- cond, when & unless
condis the multi-branch conditional;whenandunlesscover the one-sided cases. - case dispatch
casepicks a branch by comparing one value against literal keys witheql. - Recursion basicsTwo recursions over the same list of rainfall readings, differing in where the answer accumulates.
Functions
- defun & docstringsDefines a hiking-pace function whose docstring travels with it, then calls it with and without the optional argument.
- Keyword argumentsA
brew-teafunction with three&keyparameters, called three ways. - Multiple valuesSplits a lap time into minutes and seconds, then contrasts
floorwithtruncate.
Higher-order
- mapcar & reduceAdds tax to a list of cart prices with
mapcar, then totals them withreduce. - Filtering with predicatesFilters a list of bird counts three ways and tallies a fourth, without changing the original list.
- sort with :keySorts an alist of cheese wheels by weight and by name, copying first because
sortis destructive.
Collections
- Hash tablesA spice shelf keyed by strings, which needs
:test 'equalrather than the default. - Alists & plistsLooks up an area code in an association list and a field in a property list, and says when each shape fits.
- Vectors & arraysReads daily temperatures from a
#(...)literal and writes into an array built bymake-array.
Structures & CLOS
- defstruct
defstructdeclares a record and generates its constructor, accessors, and copier for free. - defclassA CLOS class spells out each slot's
:initarg,:accessor, and:initformby hand. - Generic functionsOne
defgeneric, twodefmethodbodies, and the argument's class picks which body runs.
Format & macros
- format directives
formatdirectives are a miniature language for assembling strings. - defmacroA macro rewrites source into other source before any of it is evaluated.
- Backquote templatesBackquote builds a list template; comma and comma-at fill in the holes.
Encore
- word_freq.lispCounts word frequencies in an inline passage and prints a ranked table.
- grade_report.lispTurns a list of students and test scores into per-student grades and a class summary.
- temp_stats.lispReports a week of daily high temperatures with min, max, mean, and a text bar chart.