配列中の要素を探す - lisp-cookbook-ja/common-lisp GitHub Wiki
配列中の要素を探す
position もしくは、 find を利用することにより配列から要素を検索することができます。
position は指定された要素が見つかればその位置を、見つからなければnilを返します。
find は指定された要素が見つかればその要素を、見つからなければnilを返します。
両者とも :test キーワードに比較用関数を指定することが可能です。
(let ((a #("apple" 10 "orange" #("lemon" "vine")))
(test #'equal))
(list
(position "apple" a :test test)
(position 10 a :test test)
(position #("lemon" "vine") a :test test)
(position #("fruit") a :test test)))
;=> (0 1 NIL NIL)
(let ((a #("apple" 10 "orange" #("lemon" "vine")))
(test #'equalp)) ;equalpに変更
(list
(position "apple" a :test test)
(position 10 a :test test)
(position #("lemon" "vine") a :test test)
(position #("fruit") a :test test)))
;=> (0 1 3 NIL)
(let ((a #("apple" 10 "orange" #("lemon" "vine")))
(test #'equalp))
(list
(find "apple" a :test test)
(find 10 a :test test)
(find #("lemon" "vine") a :test test)
(find #("fruit") a :test test)))
;=> ("apple" 10 #("lemon" "vine") NIL)