Subject: Re: Get subseq of alist using regexp mathcing on cars
From: Erik Naggum <erik@naggum.net>
Date: 2000/07/26
Newsgroups: comp.lang.lisp
Message-ID: <3173631943791755@naggum.net>
* "Bruce Lambert" <lambertb@uic.edu>
| This is what I came up with myself. It seems to work in the simple
| case where the cars are all single-character symbols. This will
| work for my current application. It could, of course, be much
| fancier. I still welcome suggestions.
I must admit to not having understood your original request, as
"assoc" is just not what you're asking for.
| "Takes a regular expression and an alist and returns the subsequence
| of alist whose cars match the regexp. Assume cars are all symbols
| whose print-names are single characters."
It doesn't really assume that. The cars may be one-character
strings, characters, or symbols with one-character names.
(defun regexp-subseq (sequence regexp &key (key #'car))
(destructuring-bind (match &optional (start . end))
(multiple-value-list
(match-regexp regexp
(map 'string (compose #'character key) sequence)
:return :index))
(if match
(subseq sequence start end)
nil)))
compose is either a macro or a function that composes functions. In
this case, it produces (lambda (x) (character (funcall key x))).
#:Erik
--
If this is not what you expected, please alter your expectations.