假设您有多个列表或数组,为了示例,我们假设有两个:
(defparameter *arr* #(1 2 3))
(defparameter *list* '(4 5 6))
Run Code Online (Sandbox Code Playgroud)
您可以使用或关键字loop
来覆盖它们:across
in
(loop for elem across *arr* do (format t "~a" elem))
=> 123
(loop for elem in *list* do (format t "~a" elem))
=> 456
Run Code Online (Sandbox Code Playgroud)
我希望能够loop
使用相同的语法来遍历这些数组或列表。我正在使用 SBCL,执行速度是一个问题。
being the elements of
这种语法很好,因为无论其参数是 alist
或 ,它都可以工作array
。
(loop for elem being the elements of *arr* do (format t "~a" elem))
=> 123
(loop for elem being the elements of *list* …
Run Code Online (Sandbox Code Playgroud) 我有两个带有填充指针的向量。我需要merge
这些向量,因此需要一个仍然具有填充指针的新向量。
(defparameter *a* (make-array 3 :fill-pointer 3
:initial-contents '(1 3 5)))
(defparameter *b* (make-array 3 :fill-pointer 3
:initial-contents '(0 2 4)))
(type-of *a*)
;;=> (VECTOR T 6)
;; Pushing new elements works as intended.
(vector-push-extend 7 *a*)
(vector-push-extend 6 *b*)
;; Now we create a new vector by merging *a* and *b*.
(defparameter *c* (merge 'vector *a* *b* #'<))
;;=> #(0 1 2 3 4 5 6 7)
(type-of *c*)
;;=> (SIMPLE-VECTOR 8)
;; The type of this …
Run Code Online (Sandbox Code Playgroud) 我需要在通用方法中返回矩形的坐标列表。坐标是类“购物车”实例。
我尝试用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 …
Run Code Online (Sandbox Code Playgroud)