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!

Useful definitions

Common Lisp Cheat Sheet by boechat107 - Cheatography.com Created Date: 4642Z. A cheat sheet providing Common Lisp basics & synthax in the form of a.lispfile made of commented examples. (For people who already know about programming concepts). This work is licensed to you under version 2 of the GNU General Public License.Alternatively, you may choose to receive this work under any other license that grants the right to use, copy, modify, and/or distribute the work, as long as that license imposes the restriction that derivative works have to grant the same rights and impose the same restriction.

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'.

Cheat

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

Cheat

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

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

Sheet

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:

Libraries

  • 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

I wanted to get the hang of Lispy thanks to Leo Vivier’s presentation at EmacsSF, but there are a lot of keyboard shortcuts to explore. In Karl Voit’s demo of Org Mode at GLT21, he showed how he uses Hydra to make cheat sheets. That makes perfect sense, of course, as Hydra can display text and allow you to run commands while the text is displayed. I wanted to make a Hydra that would show me categorized commands to make it easier to look up and eventually remember them. I also wanted to skip the commands that I already knew or that I didn’t want to focus on just yet.

Fortunately, the function reference had a link to the Org file used to generate it. I copied the tables, merged them together, named them with #+NAME: bindings, replaced the links with plain text, and added a third column with the category I wanted to put commands into.

#bindings
keyfunctioncolumn
<lispy-barf
Alispy-beginning-of-defun
jlispy-down
Zlispy-edebug-stop
Blispy-ediff-regions
Glispy-goto-local
hlispy-left
Nlispy-narrow
ylispy-occur
olispy-other-mode
Jlispy-outline-next
Klispy-outline-prev
Plispy-paste
llispy-right
Ilispy-shifttab
>lispy-slurp
SPClispy-space
xBlispy-store-region-and-buffer
ulispy-undo
klispy-up
vlispy-view
Vlispy-visit
Wlispy-widen
Dpop-tag-mark
xsee
Lunbound
Uunbound
Xunbound
Yunbound
Hlispy-ace-symbol-replaceEdit
clispy-cloneEdit
Clispy-convoluteEdit
nlispy-new-copyEdit
Olispy-onelineEdit
rlispy-raiseEdit
Rlispy-raise-someEdit
lispy-spliceEdit
Slispy-stringifyEdit
ilispy-tabEdit
xjlispy-debug-step-inEval
xelispy-edebugEval
xTlispy-ertEval
elispy-evalEval
Elispy-eval-and-insertEval
xrlispy-eval-and-replaceEval
plispy-eval-other-windowEval
qlispy-ace-parenMove
zlispy-knightMove
slispy-move-downMove
wlispy-move-upMove
tlispy-teleportMove
Qlispy-ace-charNav
lispy-ace-subwordNav
alispy-ace-symbolNav
blispy-backNav
dlispy-differentNav
flispy-flowNav
Flispy-followNav
glispy-gotoNav
xblispy-bind-variableRefactor
xflispy-flattenRefactor
xclispy-to-condRefactor
xdlispy-to-defunRefactor
xilispy-to-ifsRefactor
xllispy-to-lambdaRefactor
xulispy-unbind-variableRefactor
Mlispy-multilineOther
xhlispy-describeOther
mlispy-mark-listOther

Lisp Cheat Sheet

I wrote this Emacs Lisp code with the header arguments #+begin_src emacs-lisp :var bindings=bindings :colnames yes:

Emacs Lisp Cheat Sheet

Here’s the result:

Cheat

Figure 1: Hydra-based cheat sheet

I’m experimenting with having my Windows key be F14 if tapped and Super_L if held down. I use KDE, so I disabled the Applications shortcut with:

and then used xcape -e 'Super_L=F14' to make it work.

Looking forward to getting the hang of this!





Comments are closed.