[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Lost in Loop

William James

10/24/2015 3:37:00 PM

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)

--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant.... If he has any principles, he keeps
them well concealed.... He is, to the extent of his abilities, exactly like
the next citizen, who, he trusts, is trying to be exactly like him: a faceless,
characterless putty-man. --- Father Feeney; "Should Hate Be Outlawed?"
3 Answers

William James

10/24/2015 5:38:00 PM

0

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]

William James

10/24/2015 9:59:00 PM

0

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)

Wrong output. And let's make it case-insensitive.

(use srfi-13 :only (string-contains-ci))

(define (build-book-index text books)
(filter-map
(^b (cond ((string-contains-ci text b) => (cut list <> b))
(else #f)))
books))

(build-book-index "foo.tiger lillies.bar.Arthur Gordon Pym.what"
'("whammo" "Tiger Lillies" "Arthur Gordon Pym"))

===>
((4 "Tiger Lillies") (22 "Arthur Gordon Pym"))

William James

10/24/2015 10:54:00 PM

0

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"]]