Ели*_*кая 2 evaluation common-lisp quote
我需要在通用方法中返回矩形的坐标列表。坐标是类“购物车”实例。
我尝试用make-instance将其退回
(defclass line ()
((start :initarg :start :accessor line-start)
(end :initarg :end :accessor line-end)))
(defmethod print-object ((lin line) stream)
(format stream "[LINE ~s ~s]"
(line-start lin) (line-end lin)))
(defclass cart ()
((x :initarg :x :reader cart-x)
(y :initarg :y :reader cart-y)))
(defmethod print-object ((c cart) stream)
(format stream "[CART x ~d y ~d]"
(cart-x c) (cart-y c)))
(setq lin (make-instance 'line
:start (make-instance 'cart :x 4 :y 3)
:end (make-instance 'cart :x 7 :y 5)))
(defgeneric containing-rect (shape))
(defmethod containing-rect ((l line))
(let ((x1 (cart-x (line-start l)))
(y1 (cart-y (line-start l)))
(x2 (cart-x (line-end l)))
(y2 (cart-y (line-end l))))
(cond ((= x1 x2)
'((make-instance 'cart :x (1- x1) :y y1)
(make-instance 'cart :x (1+ x1) :y y1)
(make-instance 'cart :x (1- x2) :y y2)
(make-instance 'cart :x (1+ x2) :y y2)))
((= y1 y2)
'((make-instance 'cart :x x1 :y (1- y1))
(make-instance 'cart :x x1 :y (1+ y1))
(make-instance 'cart :x x2 :y (1- y2))
(make-instance 'cart :x x2 :y (1+ y2))))
(t
(rect '((make-instance 'cart :x x1 :y y1)
(make-instance 'cart :x x1 :y y2)
(make-instance 'cart :x x2 :y y2)
(make-instance 'cart :x x2 :y y1)))))))
(print (containing-rect lin))
Run Code Online (Sandbox Code Playgroud)
我想make-instance
应该为一个实例分配一个实例
所以我得到不正确的结果
((MAKE-INSTANCE 'CART :X X1 :Y Y1) (MAKE-INSTANCE 'CART :X X1 :Y Y2)
(MAKE-INSTANCE 'CART :X X2 :Y Y2) (MAKE-INSTANCE 'CART :X X2 :Y Y1))
Run Code Online (Sandbox Code Playgroud)
但我需要这样的输出
([CART x 4 y 3] [CART x 4 y 3] [CART x 4 y 3] [CART x 4 y 3])
Run Code Online (Sandbox Code Playgroud)
如果您quote
有任何评价,则不会进行评估。
这个:
'((make-instance 'cart :x (1- x1) :y y1)
(make-instance 'cart :x (1+ x1) :y y1)
(make-instance 'cart :x (1- x2) :y y2)
(make-instance 'cart :x (1+ x2) :y y2))
Run Code Online (Sandbox Code Playgroud)
是一个包含四个文字列表的文字列表,每个文字列表以symbol开头MAKE-INSTANCE
,然后具有一个list (QUOTE CART)
,依此类推。这正是您所看到的结果。
您似乎想对此进行实际评估。最简单的方法是这样做并列出清单:
(list (make-instance 'cart :x (1- x1) :y y1)
(make-instance 'cart :x (1+ x1) :y y1)
(make-instance 'cart :x (1- x2) :y y2)
(make-instance 'cart :x (1+ x2) :y y2))
Run Code Online (Sandbox Code Playgroud)
这与引用内容根本不同。