小编Tho*_*ier的帖子

无差别地循环数组或列表

问题

假设您有多个列表或数组,为了示例,我们假设有两个:

(defparameter *arr* #(1 2 3))
(defparameter *list* '(4 5 6))
Run Code Online (Sandbox Code Playgroud)

您可以使用或关键字loop来覆盖它们:acrossin

(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)

sbcl common-lisp

5
推荐指数
1
解决办法
3298
查看次数

将两个向量与填充指针合并到结果向量中

我有两个带有填充指针的向量。我需要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)

merge vector common-lisp fill-pointer

3
推荐指数
1
解决办法
82
查看次数

从通用方法返回实例列表

我需要在通用方法中返回矩形的坐标列表。坐标是类“购物车”实例。

我尝试用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)

evaluation common-lisp quote

2
推荐指数
1
解决办法
51
查看次数

标签 统计

common-lisp ×3

evaluation ×1

fill-pointer ×1

merge ×1

quote ×1

sbcl ×1

vector ×1