Youssef CHARIF
Youssef CHARIF

Reputation: 7

Search function in lisp

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.

Upvotes: 0

Views: 447

Answers (1)

Sylwester
Sylwester

Reputation: 48745

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")

Upvotes: 1

Related Questions