From Newsgroup: comp.lang.lisp
Pascal Bourguignon wrote:
I had an approach working but it looked horrid so I thought I would try loop. I came up with this:
(defun build-book-index (text books)
(loop for book in books
when (search (string-downcase book) (string-downcase text)) collect
(list it book)))
Of course, ...collect it ... worked but the version above gave:
in: LAMBDA NIL
; (LIST IT BOOK)
; caught WARNING: undefined variable: IT
Is this expected?
Yes.
How can I get round it?
Not using IT.
(defun build-book-index (text books)
(loop
for book in books
for my-it = (search (string-downcase book) (string-downcase text))
when my-it collect (list my-it book)))
It's shorter in Gauche Scheme.
(use srfi-13) ;; string-contains
(define (build-book-index text books)
(filter-map
(lambda (book)
(let1 p (string-contains-ci text book)
(and p (list p book))))
books))
(build-book-index "a foo or a bar" '("FOO" "bar" "zoom"))
===>
((2 "FOO") (11 "bar"))
--- Synchronet 3.21d-Linux NewsLink 1.2