質問

In Common Lisp, what is more idiomatic way to check if an element is in the list? Currently I am checking if "get" belongs to the list like this:

(some #'(lambda (x) (string= x "get")) '("get" "update" "delete" "replace"))
役に立ちましたか?

解決

Use member function if you need to find nil in the list:

[1]> (member nil '(1 nil 2 (nil)))
    (NIL 2 (NIL))
[2]> (member nil '(1 2 (nil)))
    NIL
[3]> (member nil '(1 2 (nil) nil))
   (NIL)
[4]> (member '(nil) '(1 2 (nil) nil))
   NIL
[5]> (member '(nil) '(1 2 (nil) nil) :test #'equal)
   ((NIL) NIL)
[6]> (member "asd" '(1 2 "asd" (nil) nil) :test #'equal)
   ("asd" (NIL) NIL)

他のヒント

Find will return the item if it is in the list, else it will return nil

(find 'a '(1 a 2 f))

Note that if you want to use a different criteria for the search you can use :key and :test. For example the following checks to see if any of the symbols have the name "JAM". We use the :key to get the symbol-name and the :test as we have to use #'equal to compare strings.

(find "JAM" '(a b c jam) :key #'symbol-name :test #'equal)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top