Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find the position of an element in a string vector in common lisp

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
like image 912
Faheem Mitha Avatar asked Dec 22 '22 00:12

Faheem Mitha


1 Answers

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.

like image 150
Ramarren Avatar answered Feb 22 '23 23:02

Ramarren