How to define constant like this in lisp?

2019-07-01 20:13发布

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?

6条回答
看我几分像从前
2楼-- · 2019-07-01 20:14

Use defconstant:

 (defconstant name initial-value-form [ documentation-string ])

For example:

(defconstant myvar 5)
查看更多
我只想做你的唯一
3楼-- · 2019-07-01 20:26

It would be more idiomatic in Lisp to just use symbols. Typically as self-evaluating keyword symbols:

(defparameter *chess-pieces*
  '(:EMPTY :PAWN :KNIGHT :BISHOP :ROOK :QUEEN :KING :BPAWN))

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:

> (setf c4 queen)

> c4
6

vs.

> (setf c4 :queen)

> c4
:queen

In the latter example the symbol value is self-descriptive.

查看更多
叼着烟拽天下
4楼-- · 2019-07-01 20:26

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:

(destructuring-bind (EMPTY PAWN KNIGHT BISHOP ROOK QUEEN KING BPAWN)
    (range 8)
  #| do stuff |#)

However, enumerations like this are seldomly used in Lisp code, since keywords (or other symbols) provide similar functionality.

查看更多
冷血范
5楼-- · 2019-07-01 20:28

What about writing a macro to do this? An example:

(defmacro defenum (&rest args)
  (let ((counter 0))
    `(progn
       ,@(mapcar (lambda (arg)
                   (cond ((symbolp arg)
                          (prog1
                              `(defparameter ,arg ,counter)
                            (incf counter)))
                         ((listp arg)
                          (prog1
                              `(defparameter ,(first arg) ,(second arg))
                            (setf counter (1+ (second arg)))))
                         (t (error "broken defenum"))))
                 args))))

Examples of usage:

(defenum x (y 2) z)

(defenum EMPTY PAWN KNIGHT BISHOP ROOK QUEEN KING BPAWN)

(it's probably very easy to improve on this example :-) - I prefer defparameter to defconstant so that's something you may want to change)

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-07-01 20:30

Here's one way to do it:

(let ((c 0))
  (dolist (piece '(EMPTY PAWN KNIGHT BISHOP ROOK QUEEN KING BPAWN))
    (eval `(defconstant ,piece ,c)) 
    (incf c)))
查看更多
Ridiculous、
7楼-- · 2019-07-01 20:36

One can use macrolet to compute a form which sets a bunch of constants:

(macrolet ((defenumvalues (&rest names)
             `(progn
                ,@(loop for n in names and i from 0
                        collect `(defconstant ,n ,i))
                (values))))
  (defenumvalues empty pawn knight bishop rook queen king bpawn))

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.

查看更多
登录 后发表回答