typestar

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.

Start this tour

Values & bindings

  • defparameter & defvarTwo ways to make a global: defparameter overwrites, defvar backs off.
  • let & let*let binds 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 with reverse and append.
  • QuotingA quote is the difference between running (+ 2 3) and holding on to it.

Control flow

  • cond, when & unlesscond is the multi-branch conditional; when and unless cover the one-sided cases.
  • case dispatchcase picks a branch by comparing one value against literal keys with eql.
  • 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-tea function with three &key parameters, called three ways.
  • Multiple valuesSplits a lap time into minutes and seconds, then contrasts floor with truncate.

Higher-order

  • mapcar & reduceAdds tax to a list of cart prices with mapcar, then totals them with reduce.
  • 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 sort is destructive.

Collections

  • Hash tablesA spice shelf keyed by strings, which needs :test 'equal rather 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 by make-array.

Structures & CLOS

  • defstructdefstruct declares a record and generates its constructor, accessors, and copier for free.
  • defclassA CLOS class spells out each slot's :initarg, :accessor, and :initform by hand.
  • Generic functionsOne defgeneric, two defmethod bodies, and the argument's class picks which body runs.

Format & macros

  • format directivesformat directives 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.

The other Lisp tours