Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.forth > #23612

Re: Book titles

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Book titles
Date 2013-06-14 19:31 +0000
Organization A noiseless patient Spider
Message-ID <kpfr2c$v3n$1@dont-email.me> (permalink)
References <kk043d$363$1@dont-email.me>

Show all headers | View raw


WJ 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)))
> 
> Factor:
> 
> USING: locals ascii sequences.extras ;
> 
> :: build-book-index ( books text -- seq )
>   text >upper :> utext
>   { } books
>   [| book |  book >upper utext start [ book 2array suffix ] when* ]
>   each
> ;
> 
> { "Typee" "Mardi" "Pierre" "x" } "pierretypeemardi" build-book-index .
> 
> { { 6 "Typee" } { 11 "Mardi" } { 0 "Pierre" } }

Ruby:

text = "pierretypeemardi"
["Typee","Mardi","Pierre","x"].map{|book|
  i = text.index( book.downcase )
  i and [i, book] or nil}.compact
    ==>[[6, "Typee"], [11, "Mardi"], [0, "Pierre"]]

Can this be done in ANS Forth?

Back to comp.lang.forth | Previous | Next | Find similar


Thread

Re: Book titles "WJ" <w_a_x_man@yahoo.com> - 2013-06-14 19:31 +0000

csiph-web