From: Albert Reiner
Subject: find substring
Date: 
Message-ID: <m1vfmbjotk.fsf@reiner.chello.at>
Hi,

I cannot seem to find in the HyperSpec or CLtL2 a way of checking whether some
string is a substring of another one.  

    (is-substring-of "fgh" "abcdefghijkl")  ==> true
    (is-substring-of "xyz" "abcdefghijkl")  ==> nil

Of course it is easy to write such a function, but I have the feeling that I
am simply not looking in the right places. (I am using CMUCL, BTW.)

Thanks for any light you can shed,

Albert.

From: Marco Antoniotti
Subject: Re: find substring
Date: 
Message-ID: <Ra8Xb.25$IJ5.3392@typhoon.nyu.edu>
(search "fgh" "abcdefghijkl")

Note that SEARCH works on SEQUENCEs, therefore you can use it on lists 
as well

(search '(1 2 3) '(0 9 8 4 3 1 2 3 6))

SEARCH is standard ANSI.  As a hint about how to wander around the ANSI 
stuff note the following.  When looking for functions dealing with 
strings (vectors, lists, etc etc), try the SEQUENCE section.  A lot of 
goodies are actually there.

Cheers
--
Marco



Albert Reiner wrote:
> Hi,
> 
> I cannot seem to find in the HyperSpec or CLtL2 a way of checking whether some
> string is a substring of another one.  
> 
>     (is-substring-of "fgh" "abcdefghijkl")  ==> true
>     (is-substring-of "xyz" "abcdefghijkl")  ==> nil
> 
> Of course it is easy to write such a function, but I have the feeling that I
> am simply not looking in the right places. (I am using CMUCL, BTW.)
> 
> Thanks for any light you can shed,
> 
> Albert.
From: Pascal Bourguignon
Subject: Re: find substring
Date: 
Message-ID: <87wu6qet8m.fsf@thalassa.informatimago.com>
Marco Antoniotti <·······@cs.nyu.edu> writes:

> (search "fgh" "abcdefghijkl")
> 
> Note that SEARCH works on SEQUENCEs, therefore you can use it on lists
> as well
> 
> (search '(1 2 3) '(0 9 8 4 3 1 2 3 6))
> 
> SEARCH is standard ANSI.  As a hint about how to wander around the
> ANSI stuff note the following.  When looking for functions dealing
> with strings (vectors, lists, etc etc), try the SEQUENCE section.  A
> lot of goodies are actually there.

And note too that you can mix sequence types:

[53]> (search '( #\a #\b #\c )   #( #\_ #\+ #\a #\b #\c #\- #\! ))
2
[58]> (search '( #\a #\b #\c )   "toto-abc-titi")
5


-- 
__Pascal_Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/
From: Albert Reiner
Subject: Re: find substring
Date: 
Message-ID: <m1ad3mu7ap.fsf@reiner.chello.at>
Thanks to all of you, those hints have been *most* helpful.

(I did look at the vector and array stuff, but forgot about sequences.  And
for some reason I looked for a function like find-... rather than search.)

BTW, I have another question, this time on Lisp style: I am aware of the
*special-variable* convention.  Are there any similar conventions I should
adhere if I want my code to be easily readable for others?

Thanks again,

Albert.
From: Edward O'Connor
Subject: Re: find substring
Date: 
Message-ID: <dd65eayep9.fsf@oecpc11.ucsd.edu>
> BTW, I have another question, this time on Lisp style: I am aware of
> the *special-variable* convention. Are there any similar conventions I
> should adhere if I want my code to be easily readable for others?

               http://www.cliki.net/Naming%20conventions

-- 
Edward O'Connor
···@oconnor.cx
From: Henrik Motakef
Subject: Re: find substring
Date: 
Message-ID: <86isiak1fa.fsf@pokey.internal.henrik-motakef.de>
Albert Reiner <······@chello.at> writes:

> BTW, I have another question, this time on Lisp style: I am aware of the
> *special-variable* convention.  Are there any similar conventions I should
> adhere if I want my code to be easily readable for others?

Several, see <http://www.cliki.net/Naming%20conventions>
From: Thomas A. Russ
Subject: Re: find substring
Date: 
Message-ID: <ymiu11ptwwy.fsf@sevak.isi.edu>
Albert Reiner <······@chello.at> writes:

> 
> Thanks to all of you, those hints have been *most* helpful.
> 
> (I did look at the vector and array stuff, but forgot about sequences.  And
> for some reason I looked for a function like find-... rather than search.)
> 
> BTW, I have another question, this time on Lisp style: I am aware of the
> *special-variable* convention.  Are there any similar conventions I should
> adhere if I want my code to be easily readable for others?

There are a few other items.  One is that, by convention, lisp symbols
tend to have longish, descriptive names with the words separated by
hyphens, for example solve-world-hunger rather than solve_world_hunger
or swh or solveWorldHunger.

Close parentheses go together at the end of lines of code and not on
separate lines.

Code needs to be indented properly.  Usually people let their editor do
this for them.

Other than that, things are some common sense guidelines, like breaking
lines of code in places that make logical sense.  For example, with
multiple assignments it is better to do

        (setq variable1 value1
              variable2 value2
              ...)

than to let auto-fill loose and get something like

        (setq variable1 value1 variable2
              value2 very-long-variable-name3
              value3)



-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Coby Beck
Subject: Re: find substring
Date: 
Message-ID: <c0i6kq$oqe$1@otis.netspace.net.au>
"Albert Reiner" <······@chello.at> wrote in message
···················@reiner.chello.at...
> Hi,
>
> I cannot seem to find in the HyperSpec or CLtL2 a way of checking whether
some
> string is a substring of another one.
>
>     (is-substring-of "fgh" "abcdefghijkl")  ==> true
>     (is-substring-of "xyz" "abcdefghijkl")  ==> nil
>
> Of course it is easy to write such a function, but I have the feeling that
I
> am simply not looking in the right places. (I am using CMUCL, BTW.)

(search "waldo" "where the heck's waldo?")

=> 17

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")
From: Rob Warnock
Subject: Re: find substring
Date: 
Message-ID: <p7GcnX-NMslFRLHdXTWc-w@speakeasy.net>
Coby Beck <·····@mercury.bc.ca> wrote:
+---------------
| "Albert Reiner" <······@chello.at> wrote in message
| > I cannot seem to find in the HyperSpec or CLtL2 a way of checking whether
| > some string is a substring of another one.
| 
| (search "waldo" "where the heck's waldo?")  =>  17
+---------------

And in cases where an anchored match is more appropriate:

    (mismatch "there" "here & there & everywhere" :start2 7)  =>  5

or for partial matches (also left-anchored):

    (mismatch "their" "here & there & everywhere" :start2 7)  =>  3

which says that only the first 3 chars matched.

SEARCH provides the {start,end}{1,2} keywords, too, which can be
helpful in avoiding intermediate garbage when doing incremental
searches, e.g.:

    (loop with target = "where's waldo? what's waldo? when's waldo?"
	  and start = 0
	  for pos = (search "waldo" target :start2 start)
      while pos
	collect pos
	do (setf start (1+ pos)))
    => (8 22 36)


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607