Simple saves
nil
->()
and
->if
(if it has 2 arguments)(loop for...
->(loop as...
(concatenate'string a b)
->(format()"~a~a"a b)
- Large numbers can be compacted by writing them in base 36 with the prefix
#36r
.
Setting variables
There are multiple ways to set a variable:
(set'x 3)
to set just one variable.(setq x 3 y 5)
to set more than one variable in succession.setf
to set places (e.g. a location in an array or hash table)- Normal variables and places can also be combined in
setf
statements.
- Normal variables and places can also be combined in
psetf
is likesetf
, but assignments are done in parallel.incf
/decf
to add/subtract from a variable.
All of the above methods also return the new value of the variable; multiple assignments return the last assigned value.
Special variables
'
& `
Sometimes, it is possible to use quote
as a variable, since 'foo
is short for (quote foo)
. To use quote
in an expression, use dotted pair notation. Lisp parses this notation like a list literal, so it must be followed by exactly one literal, identifier, or form.
; Example:
(dotimes(i 10)(format t"~d ~a~%"i"potato")) ; this
(dotimes'10(format t"~d ~a~%".'"potato")) ; is equivalent to this,
(dotimes'10(format t"~d ~a~%".'"potato"())) ; but this will fail to compile
The above also holds for backquote, using .`
.
+
, -
, *
, & /
The variables +
, ++
, and +++
(and their counterparts for /
and *
) store certain prior values of forms during the Lisp read-eval-print loop. Since submissions on code.golf are not run via interactive REPL, these values are never updated, but are set to specific values at the start of execution. Of particular use are *
and /
, which are both nil
.
Reference
- The Common Lisp Hyper-Spec has a useful alphabetical list of functions.
- The Implementation Notes for GNU CLISP have information specific to the version of Lisp used on code.golf.
- The Wikipedia page for
format
contains (nearly) every directive available for theformat
function, with many annotated examples and a handy table. - Directives and particular syntax details not found in the above can be found in the Hyper-Spec Section on Formatted Output .