Returning the position of a element in a string and a numerical vector, and a 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 for a string vector does not work. I assume this is because a string is itself a vector of characters. So, what can one use?
CL-USER> (position "A" #("A" "C" "G" "T"))
NIL
By default, POSITION tests the element using EQL, which is true about most sequence function that use tests, as per CLHS 17.2.1. For vectors EQL compares by identity, not contents, and two strings "A" will usually be different, even if they look the same. To compare by contents you need to pass :test #'equal
to POSITION. Or 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With