Lisp Cheat Sheet



Common Lisp is a general-purpose programming language with functions as first-class citizens. Don't worry about being purely functional, Lisp is Object Oriented too. CLOS is a very powerful object-oriented system!

Be sure to check out the Hyperpolyglot Lisp section. It's a great cheat sheet to use and very useful if you already know a variant of Lisp. As a final note, are you sure you want to learn Common Lisp? There are some modern Lisps which are interesting, e.g., Clojure, Racket, New Lisp, Qi. In an attempt to learn Lisp, I've read Land of Lisp and a few Elisp tutorials and have produced a nifty cheat sheet, from setq upto anaphoric Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts.

Variables GlobalVariables,Create&Update: (setq name value). Generally: (setq name 0 value 0 name k value k.

Useful definitions

The Common Lisp lingo is quite unique:

  • Package: Basically a namespace, a place for symbols to live
  • System: Basically a Library. A bunch of code plus some instructions how it should be treated, for example which other systems it depends on, what should be loaded and/or compiled first, etc. Not in ANSI lisp but widespread. The most common system definition tool is ASDF.
  • Modules: Deprecated and implementation-dependent
  • Quicklisp: Like NPM or Ruby Gems for ASDF Systems.

Variables

You can define, set and get variables as follows:

The 'earmuffs' are an optional convention, it means 'warning, this can change'.

setf and getf can be used for structs, object and pretty much everything.

Equality Predicates

  • (eq x y) is true if and only if x and y are the same identical object.
  • (eql x y) is true if its arguments are eq, or if they are numbers of the same type with the same value, or if they are character objects that represent the same character.
  • (equal x y) is true if its arguments are structurally similar (isomorphic) objects. A rough rule of thumb is that two objects are equal if and only if their printed representations are the same.
  • (equalp x y) is true if they are equal; if they are characters and satisfy char-equal, which ignores alphabetic case and certain other attributes of characters; if they are numbers and have the same numerical value, even if they are of different types; or if they have components that are all equalp.

Format

format is like C's printf. It will replace some wildcards in the string with the values passed. The most common use case are either print something to the screen or return the value as a string.

~A stands for Anything, it works most of the time and is the most common wildcard used. See this chapter of Practical Common Lisp for more on this.

Functions

Objects

More info: Brief Guide to CLOS.

Data Structures

Conditions and Restarts

You can use conditions pretty much the same way you would use a Try/Catch:

Lisp Slime Cheat Sheet

Instead of using handler-case you can use restart-case and specify how thaterror is restarted, so callers can choose a restart and abstract the implementation away

Callers (either direct or indirect) of a function which provides restarts can pick the restart they want by wrapping code in a handler-bind macro. handler-bind associates a condition with it's restart:

If you are not sure the restart will be there you can check by using find-restart:

You can find more info on Conditions and Restarts in this chapter of the awesome book: Practical Common Lisp.

Making errors play nice with the debugger

Use :report for that:

  • Install Roswell
  • Install Spacemacs
  • Enable the Common Lisp config

For each project, you'll need to add it to ASDF's central registry. You can do this by adding something like this to your ~/.sbclrc file (.roswell/init.lisp if you are using Roswell):

Because it's a hassle to do this everytime you make a new project, if you use a UNIX-y OS you can use a dedicated folder to contain all ASDF system definitions (.asd files)

Your central repository definition would then look like this:

Lisp Cheat Sheet Printable

Libraries

Cheat
  • You can use Quickproject to make new projects
  • You can use rove for testing
  • Alexandria is an extension to Common Lisp to provide some overall goodies, kind of like ActiveSupport for Ruby

You can use break and step and trace for debugging. Remember to enable the debug settings in the Lisp initialization file.

Once in the debugging SLIME session, you can step in, step over, and press e for evaluating a Lisp expression. More info here.

For break, just put it as a regular breakpoint in code. For step, use it to call a function:

If you want to try some print debugging you can drop a (inspect ...) in the code and the execution will stop there and you will able to inspect whatever you want.

  • More on CLOS (Common Lisp Object System)
  • Practical Common Lisp very good and in-depth introduction to Common Lisp
  • Debugging how to step-in debug your Common Lisp programs