William James
10/24/2015 10:54:00 PM
WJ wrote:
> WJ 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:
> >
> > (define (build-book-index text books)
> > (filter-map (cut string-scan text <>) books))
> >
> > (build-book-index "foo.Tiger Lillies.bar.Arthur Gordon Pym.what"
> > '("Whammo" "Tiger Lillies" "Arthur Gordon Pym"))
> >
> > ===>
> > (4 22)
>
> MatzLisp (Ruby):
>
> def build_book_index text, books
> books.map{|b| text.index b}.compact
> end
>
> build_book_index "foo.Tiger Lillies.bar.Arthur Gordon Pym.what",
> ["Whammo", "Tiger Lillies", "Arthur Gordon Pym"]
>
> ===> [4, 22]
def build_book_index text, books
books.map{|b| [text.index(b), b]}.select(&:first)
end
build_book_index "foo.Tiger Lillies.bar.Arthur Gordon Pym.what",
["Whammo", "Tiger Lillies", "Arthur Gordon Pym"]
===> [[4, "Tiger Lillies"], [22, "Arthur Gordon Pym"]]