home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS - Coast to Coast
/
simteldosarchivecoasttocoast2.iso
/
xlisp
/
xl21freq.zip
/
WILDCARD.LSP
< prev
next >
Wrap
Lisp/Scheme
|
1993-12-17
|
1KB
|
41 lines
; Wildcard Pattern matching algorithm
; * matches any substring (zero or more characters)
; ? matches any character
; ~c matches c
(defun match (pattern list)
(labels ((match1 (pattern suspect)
(cond ((null pattern) (null suspect))
((null suspect) (equal pattern '(:mult)))
((eq (first pattern) :single)
(match1 (cdr pattern) (cdr suspect)))
((eq (first pattern) :mult)
(if (null (rest pattern))
t
(do ((p (rest pattern))
(l suspect (cdr l)))
((or (null l) (match1 p l))
(not (null l))))))
((eq (first pattern) (first suspect))
(match1 (rest pattern) (rest suspect)))
(t nil)))
(explode (list)
(cond ((null list) nil)
((eq (first list) #\*)
(cons :mult (explode (rest list))))
((eq (first list) #\?)
(cons :single (explode (rest list))))
((eq (first list) #\~)
(cons (second list)
(explode (rest (rest list)))))
(t (cons (first list) (explode (rest list)))))))
(let ((pat (explode (coerce pattern 'cons))))
(mapcan #'(lambda (x) (when (match1 pat (coerce x 'cons))
(list x)))
list))))
(setq l (sort (apply #'nconc (map 'cons
#'(lambda (x) (mapcar #'string x))
*obarray*))
#'string<))