In python it's possible to do this
EMPTY, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, BPAWN = range(8)
How would you do equivalent in lisp?
In python it's possible to do this
EMPTY, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, BPAWN = range(8)
How would you do equivalent in lisp?
Use defconstant:
For example:
It would be more idiomatic in Lisp to just use symbols. Typically as self-evaluating keyword symbols:
There are reasons to define numeric values - sometimes. Especially when you need to talk to foreign functions. In Lisp you would by default use symbols.
Common Lisp does not have a real enumeration type. Using symbols in a dynamically typed language has some advantages over using numeric variables. For example during debugging the variable contents are more descriptive:
Compare:
vs.
In the latter example the symbol value is self-descriptive.
Assuming that you have defined the convenience function
range
somewhere ((defun range (n) (loop :for i :from 0 :below n :collect i))
), you could set up some local values like this:However, enumerations like this are seldomly used in Lisp code, since keywords (or other symbols) provide similar functionality.
What about writing a macro to do this? An example:
Examples of usage:
(it's probably very easy to improve on this example :-) - I prefer
defparameter
todefconstant
so that's something you may want to change)Here's one way to do it:
One can use
macrolet
to compute a form which sets a bunch of constants:Alternative: writing a global macro with
defmacro
.The general advantage of using a macro (local or global) to define a bunch of constants: the compiler expands the expression and then sees a bunch of
cl:defconstant
forms.