When I want to know whether a list includes a value, I can use member function.

(member "a" '("a" "ā" "á" "ǎ" "à"))

Is there similar way to check if a vector includes a value?

(member "a" ["a" "ā" "á" "ǎ" "à"])
;=> nil

You can use cl-position, from the cl-seq package:

ELISP> (cl-position "a" ["a" "ā" "á" "ǎ" "à"] :test #'equal)
0 (#o0, #x0, ?\C-@)
ELISP> (cl-position "b" ["a" "ā" "á" "ǎ" "à"] :test #'equal)

If it returns an integer, your vector contains the element. If it returns nil, the vector does not.

You have to use a test function that can compare strings; the default test won't treat two strings containing the same characters as equivalent; you can use #'test, as above, to do this.

Unfortunately, cl-member also requires a list, but cl-position definitely works, with the caveat that it returns the index the element is at, and not t.


You can always convert the vector to a list, and then test with member:

(member "a" (append ["a" "ā" "á" "ǎ" "à"] ()))

Because this is the normal list function member, if the value is a member the return value is the "tail" of the vector, as the sublist whose car is that value.

For example, (member "á" (append ["a" "ā" "á" "ǎ" "à"] ())) returns ("á" "ǎ" "à"). In some contexts, this can be an advantage.

Also the built-in function append is quite fast in this context. However, this does create a list, which means there is a cost for consing.


There are also options in the in-built seq.el library:

(require 'seq)
(seq-position ["a" "ā" "á" "ǎ" "à"] "a")
=> 0

(seq-contains ["a" "ā" "á" "ǎ" "à"] "a")
=> "a"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.