In this StackOverFlow question, I created an employee database and provided a select-by-first function. How can I write a select-by-first-pattern where the record is returned if any part of the string/symbol matches?
(select-by-first-pattern 'ev); Returns all records containing "ev" (eg. Steve)
Here's the code needed to construct the database: (UPDATED: Included suggested solution)
(require 'cl) (defvar *emp-db* nil) (defun add-record (emp) (push emp *emp-db*)) (defstruct employee age first-name last-name sex children) (add-record (make-employee)) (add-record (make-employee :age 34 :last-name 'farquharson :first-name 'alice :sex 'female)) (add-record (make-employee :age 43 :last-name 'jobs :first-name 'steve :sex 'male)) (add-record (make-employee :age 53 :last-name 'ballmer :first-name 'steve :sex 'male)) (defun select-by-first (first-name) (remove-if-not #'(lambda (employee) (equal (employee-first-name employee) first-name)) *emp-db*)) ;; ---------- Answer below ---------- ;; (defun select-by-first-pattern (first-name) (remove-if-not #'(lambda (employee) (if (string-match first-name (symbol-name (employee-first-name employee))) t nil)) *emp-db*)) (print (select-by-first-pattern "al")); Returns alice's record (print (select-by-first-pattern "ev")); Returns records of the two Steve's (print (select-by-first-pattern "ee")); Returns nil