76/ 结构
1 > (defstruct point x y);;;;定义结构形式
POINT
5 > (setf p (make-point :x 0 :y 0));;;每一个make-point的调用,会返回一个新的point
#S(POINT :X 0 :Y 0);;;p点赋值0,0
5 > (setf p (make-point :x 3 :y 0));;p点赋值3,0
#S(POINT :X 3 :Y 0)
5 > (point-x p);;取出p点x位置值
3
5 > p
#S(POINT :X 3 :Y 0);;测试p点x,y位置值
5 > (setf (point-y p) 2);;对p点y位置重新赋值
2
5 > p
#S(POINT :X 3 :Y 2);;测试p点x,y位置值
5 > (point-p p);;测试p是否为一个点
T
5 > (typep p 'point);;测试p是否为一个点
T
6 > (make-point);;;查询点的结构形式,point为结构形式名
#S(POINT :X NIL :Y NIL)
6 > p
#S(POINT :X 3 :Y 2)
5 > (defstruct polemic;;;定义结构名
(type (progn
(format t "What kind of polemic was it? ");;;结构形式体
(read)))
(effect nil))
POLEMIC
5 > (make-polemic);;;查询定义的结构形式,一段文字及一个输入值
What kind of polemic was it? ;;结果1
r;;键盘输入
#S(POLEMIC :TYPE R :EFFECT NIL);;结果2
6 > (make-polemic :effect "good");;;默认值替换,效果
What kind of polemic was it? hao
#S(POLEMIC :TYPE HAO :EFFECT "good")
6 > (make-polemic :effect "good");;;中文测试
What kind of polemic was it? 好
#S(POLEMIC :TYPE ? :EFFECT "good")
6 > (defun print-point (p stream depth);;;定义函数名及形参.;;;这个函数需要接受三个实参:要被印出的结构,在哪里被印出,第三个参数通常可以忽略。
(format stream "#<~A, ~A>" (px p) (py p))) ;;;打印的结构及值
PRINT-POINT
6 > (defstruct (point (:conc-name p);;;:conc-name关键字参数指定了要放在字段前面的名字,并用这个名字来生成存取函数。预设是point-;现在设定成p。
(:print-function print-point));;:print-function是在需要显示结构出来看时,指定用来打印结构的函数 ── 需要显示的情况比如,要在顶层显示时。
(x 0)
(y 0))
POINT
6 > (make-point)
#<0, 0>
6 >