配列同士の和・差・積を取る - lisp-cookbook-ja/common-lisp GitHub Wiki
配列同士の和・差・積を取る FIXME
集合を扱う関数は標準ではリストにのみ準備されていますので、配列の場合は自作するか、リスト用の関数の結果を型変換することが考えられるでしょう。
オペレータを作成するマクロ
(defmacro list-op->seq-op (result-type list-op-name)
`(defun ,(intern
(concatenate 'string (string result-type)
"-"
(string list-op-name)))
(x y &rest keys)
(COERCE
(apply #',list-op-name (coerce x 'list) (coerce y 'list)
keys)
',RESULT-TYPE)))
上記で作成したものを試してみる。cf. リスト同士の和・差・積を取る
(list-op->seq-op vector set-difference)
(list-op->seq-op vector union)
(list-op->seq-op vector intersection)
(vector-union #(1 3 5 7) #(2 4 6 8)) ;=> #(8 6 4 2 1 3 5 7)
(vector-union #(1 2 3 4) #(3 4 5 6) :test #'eql) ;=> #(6 5 1 2 3 4)
(vector-set-difference #(1 3 5 7) #(2 4 6 8)) ;=> #(7 5 3 1)
(vector-set-difference #(1 2 3 4) #(3 4 5 6)) ;=> #(2 1)
(vector-intersection #(1 3 5 7) #(2 4 6 8)) ;=> #()
(vector-intersection #(i 2 3 4) #(3 4 5 6)) ;=> #(4 3)