LoginSignup
2
0

More than 3 years have passed since last update.

AutoLISP リスト操作いろいろ

Last updated at Posted at 2019-03-26

リストを調べる

指定要素と同じ要素がリスト内に存在するか調べる

(defun list:Contains (lst e) (if (member e lst) T))
;; < Example >
;; (list:Contains '("A" "B" "C" "D" "E" "F") "C")
;; -> T

指定要素と同じ要素がリスト内に存在しないか調べる

(defun list:!Contains (lst e) (null (member e lst)))
;; < Example >
;; (list:!Contains '("A" "B" "C" "D" "E" "F") "C")
;; -> nil

リスト内の指定要素の数を数える

(defun list:Count (lst e)
    (cond ((null (member e lst)) 0)
          (T (+ 1 (list:Count (cdr (member e lst)) e)))
    )
)
;; < Example >
;; (list:Count '(0 1 2 3 4 5 6 7 8 9 2) 2)
;; -> 2

リストに含まれる指定要素の位置(インデックス番号)を取得

(defun list:IndexOf (lst e)
    (if (member e lst)
        (- (length lst) (length (member e lst)))
    )
)
;; < Example >
;; (list:IndexOf "D" '("A" "B" "C" "D" "A" "E" "F"))
;; -> 3

リストを作成

同じ要素のリストを作成

(defun list:SameElements (n e / lst) (repeat n (setq lst (cons e lst))))
;; < Example >
;; (list:SameElements 10 0)
;; -> (0 0 0 0 0 0 0 0 0 0)

リストを編集

リストから重複した要素を除去

(defun list:Distinct (lst)
    (if lst (cons (car lst) (list:Distinct (vl-remove (car lst) lst))))
)
;; < Example >
;; (list:Distinct '("A" "B" "C" "D" "A" "E" "F"))
;; -> ("A" "B" "C" "D" "E" "F")

リストから nil を除去

(defun list:RemoveNil (lst) (vl-remove-if 'null lst))

リストの最後に要素を追加

(defun list:AddLast (lst e) (append lst (list e)))

リストの始めと最後に値を追加

(defun list:AddHeadAndLast (lst s e) (append (cons s lst) (list e)))

リストの最後から2番目の値を取得

(defun list:GetLast2 (lst) (last (reverse (cdr (reverse lst)))))
;; < Example >
;; (list:GetLast2 '("A" "B" "C" "D" "E"))
;; -> "D"

リストの最後を除去する

(defun list:RemoveLast (lst) (reverse (cdr (reverse lst))))

リストの n 番目の要素以外を返す

(defun list:!Nth (lst n)
    (if (and lst (< 0 n)) 
        (cons (car lst) (list:!Nth (cdr lst) (1- n))) (cdr lst)
    )
)
;; < Example >
;; (list:!Nth '("A" "B" "C" "D" "E") 2)
;; -> ("A" "B" "D" "E")

リストの先頭から指定要素数を返す

(defun list:Take (lst n)
    (if (and lst (< 0 n)) (cons (car lst) (list:Take (cdr lst) (1- n))))
)
;; < Example >
;; (list:Take '("A" "B" "C" "D" "E") 2)
;; -> ("A" "B")

リストの先頭から指定要素数を除去

(defun list:Skip (lst n)
    (if (and l (< 0 n)) (list:Skip (cdr lst) (1- n)) lst)
)
;; < Example >
;; (list:Skip '("A" "B" "C" "D" "E") 2)
;; -> ("C" "D" "E")

リストの順番を回転

(defun list:Rotation (lst n) ;; n - 回転数
    (cond
        ;; 正の整数 - 左回転
        ((< 0 n) 
            (repeat n (setq lst (list:AddLast (cdr lst) (car l)))))
        ;; 負の整数 - 右回転
        ((> 0 n) 
             (repeat (abs n) (setq lst (cons (last lst) (list:RemoveLast lst)))))
        ;; それ以外はそのまま返す
        (T lst)
    )
)
;; < Example >
;; (list:Rotation '("A" "B" "C" "D" "E") 2)
;; -> ("C" "D" "E" "A" "B")
;; (list:Rotation '("A" "B" "C" "D" "E") -2)
;; -> ("D" "E" "A" "B" "C")
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0