Sujet : Re: Lost in Loop
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 15. Aug 2025, 09:03:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <107mpjh$10gqh$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:
Pascal Bourguignon wrote:
it to learn Lisp. Basically, for a list of book titles I wanted to
return a list of position in text and the book title, so
(build-book-index "wjhedbwjhbMarkajbdadbGenesis" books)
gave: ((21 "Genesis") (10 "Mark")).
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)))
Gauche Scheme
(use srfi-13) ;; string-contains-ci
(define (build-book-index text books)
(filter-map~ book
(let1 p (string-contains-ci text book)
(and p (list p book)))
books))
(build-book-index "foo.tiger lillies.bar.dead calm.what"
'("whammo" "Tiger Lillies" "Dead Calm"))
===>
((4 "Tiger Lillies") (22 "Dead Calm"))
Given:
(define-syntax fn*
(syntax-rules ()
[(_ (a b c ... . d) . z)
(lambda (xs)
(let-values (((a b c ... . d) (apply valuevs xs))) . z))]
[(_ (v ...) . z)
(lambda (xs) (let-values (((v ...) (apply values xs))) . z))]
[(_ (a . b) . z)
(lambda (xs) (let ((a (car xs)) (b (cdr xs))) . z)) ]
[(_ a . z) (lambda (a) . z) ] ))
(define-syntax filter-map~
(syntax-rules ()
[(_ (v0 v1 ...) expr seq0 seq1 seq2 ...)
(filter-map (lambda(v0 v1 ...) expr) seq0 seq1 seq2 ...) ]
[(_ v expr seq) (filter-map (fn* v expr) seq) ] ))
Using an anaphoric macro:
(define (build-book-index text books)
(keep book (string-contains-ci text book) (list it book)
books))
Given:
(define-macro keep
(case-lambda
((pred seq) `(filter ,pred ,seq))
((v test expr seq)
`(filter-map
(lambda(,v) (let ((it ,test)) (and it ,expr)))
,seq))
((v test seq)
`(filter (lambda(,v) ,test) ,seq))))
| Date | Sujet | # | | Auteur |
| 15 Aug 25 | Re: Lost in Loop | 1 | | B. Pym |
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal