如何在Lisp中定义这样的常量? [英] How to define constant like this in lisp?
问题描述
在python中可以做到这一点
In python it's possible to do this
EMPTY, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, BPAWN = range(8)
您将如何做等同于Lisp的操作?
How would you do equivalent in lisp?
推荐答案
在Lisp中,仅使用符号会更加惯用。通常作为自评估关键字符号:
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))
有定义数字值的原因-有时。尤其是当您需要与外部功能交谈时。在Lisp中,默认情况下将使用符号。
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没有真正的枚举类型。与使用数字变量相比,以动态类型语言使用符号具有一些优势。例如,在调试期间,变量内容更具描述性:
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:
比较:
> (setf c4 queen)
> c4
6
vs。
> (setf c4 :queen)
> c4
:queen
在后一个示例中,符号值是自描述的。
In the latter example the symbol value is self-descriptive.
这篇关于如何在Lisp中定义这样的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!