Find the position of an element in a string vector in general lisp

Returns the position of an element in a string and a numeric vector, and the character vector works using position

 CL-USER> (position #\T "ACGT") 3 CL-USER> (position 2 #(1 2 3 4)) 1 CL-USER> (position #\A #(#\A #\C #\G #\T)) 0 

The following lines for the line vector do not work. I assume this is because the string itself is a character vector. So what can I use?

 CL-USER> (position "A" #("A" "C" "G" "T")) NIL 
+4
source share
2 answers

By default, POSITION checks an element using EQL , which is true for most sequence functions that use tests according to CLHS 17.2.1 . For vectors, EQL is compared by identity, not by content, and the two strings of "A" will usually be different, even if they look the same. For content comparison, you need to pass :test #'equal to POSITION. Either string = or string-equal , which are specialized for strings and will signal an error if one of the arguments is not a string. Also string-equal is case insensitive.

+8
source

Try (position "A" #("A" "B" "C" "D") :test #'equal) .

+2
source

Source: https://habr.com/ru/post/1401318/


All Articles