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'.
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:
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:
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.
key | function | column |
---|---|---|
< | lispy-barf | |
A | lispy-beginning-of-defun | |
j | lispy-down | |
Z | lispy-edebug-stop | |
B | lispy-ediff-regions | |
G | lispy-goto-local | |
h | lispy-left | |
N | lispy-narrow | |
y | lispy-occur | |
o | lispy-other-mode | |
J | lispy-outline-next | |
K | lispy-outline-prev | |
P | lispy-paste | |
l | lispy-right | |
I | lispy-shifttab | |
> | lispy-slurp | |
SPC | lispy-space | |
xB | lispy-store-region-and-buffer | |
u | lispy-undo | |
k | lispy-up | |
v | lispy-view | |
V | lispy-visit | |
W | lispy-widen | |
D | pop-tag-mark | |
x | see | |
L | unbound | |
U | unbound | |
X | unbound | |
Y | unbound | |
H | lispy-ace-symbol-replace | Edit |
c | lispy-clone | Edit |
C | lispy-convolute | Edit |
n | lispy-new-copy | Edit |
O | lispy-oneline | Edit |
r | lispy-raise | Edit |
R | lispy-raise-some | Edit |
lispy-splice | Edit | |
S | lispy-stringify | Edit |
i | lispy-tab | Edit |
xj | lispy-debug-step-in | Eval |
xe | lispy-edebug | Eval |
xT | lispy-ert | Eval |
e | lispy-eval | Eval |
E | lispy-eval-and-insert | Eval |
xr | lispy-eval-and-replace | Eval |
p | lispy-eval-other-window | Eval |
q | lispy-ace-paren | Move |
z | lispy-knight | Move |
s | lispy-move-down | Move |
w | lispy-move-up | Move |
t | lispy-teleport | Move |
Q | lispy-ace-char | Nav |
– | lispy-ace-subword | Nav |
a | lispy-ace-symbol | Nav |
b | lispy-back | Nav |
d | lispy-different | Nav |
f | lispy-flow | Nav |
F | lispy-follow | Nav |
g | lispy-goto | Nav |
xb | lispy-bind-variable | Refactor |
xf | lispy-flatten | Refactor |
xc | lispy-to-cond | Refactor |
xd | lispy-to-defun | Refactor |
xi | lispy-to-ifs | Refactor |
xl | lispy-to-lambda | Refactor |
xu | lispy-unbind-variable | Refactor |
M | lispy-multiline | Other |
xh | lispy-describe | Other |
m | lispy-mark-list | Other |
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:
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.