Miscellaneous tips
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.
psetf
is like setf
, 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 .`
. Backquote's intended use is also quite powerful for golfing, used for constructing and splatting/unrolling lists:
(list(+ x 10)1 2 3'(foo bar))
`(,(+ x 10)1 2 3(foo bar))
(append a b)
`(,@a,@b)
+
, -
, *
, & /
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