• Re: Lost in Loop

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Wed Jun 18 10:56:39 2025
    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
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Jul 3 03:24:43 2025
    From Newsgroup: comp.lang.lisp

    Pascal Bourguignon wrote:

    Well, of course, IT can be :IT

    (loop :for i :in '( a b nil c nil d) :when i :collect :it) --> (A B C D)

    (filter values '(a b #f c #f d))
    ===>
    (a b c d)
    --- Synchronet 3.21d-Linux NewsLink 1.2