Search function in lisp

446 Views Asked by At

How can i retrieve a list of items (string) which contain a specific word from another list. Here is an Example :

(setq l '("word1_jj" "word2_mm" "word3_jj" "word4_kk"))

I wanna extract all string in which figure "_jj.

1

There are 1 best solutions below

0
On BEST ANSWER

You should make ends-with-p that takes the word and an ending. To do that you find out how many characters there is in the two strings and use subseq to make a string consisting of the last letters of the word. You can use equal to check it with the supplied argument it should match.

When you have that you can do this:

(remove-if-not (lambda (x) (ends-with-p x "_jj")) 
               '("word1_jj" "word2_mm" "word3_jj" "word4_kk"))

; ==> ("word1_jj" "word3_jj")

Alternatively you could make a make-end-predicate that returns a lambda that takes a word:

(remove-if-not (make-end-predicate "_jj") 
               '("word1_jj" "word2_mm" "word3_jj" "word4_kk"))

; ==> ("word1_jj" "word3_jj")