Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find substring

11 views
Skip to first unread message

Albert Reiner

unread,
Feb 13, 2004, 4:44:41 AM2/13/04
to
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.

Coby Beck

unread,
Feb 13, 2004, 4:50:14 AM2/13/04
to

"Albert Reiner" <alb...@chello.at> wrote in message
news:m1vfmbj...@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")

Rob Warnock

unread,
Feb 13, 2004, 9:06:16 AM2/13/04
to
Coby Beck <cb...@mercury.bc.ca> wrote:
+---------------

| "Albert Reiner" <alb...@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 <rp...@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

Marco Antoniotti

unread,
Feb 13, 2004, 12:53:53 PM2/13/04
to

(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

Pascal Bourguignon

unread,
Feb 13, 2004, 1:20:41 PM2/13/04
to
Marco Antoniotti <mar...@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/

Albert Reiner

unread,
Feb 13, 2004, 2:07:57 PM2/13/04
to
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.

Edward O'Connor

unread,
Feb 13, 2004, 2:14:26 PM2/13/04
to
> 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
t...@oconnor.cx

Henrik Motakef

unread,
Feb 13, 2004, 6:24:57 PM2/13/04
to
Albert Reiner <alb...@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>

Thomas A. Russ

unread,
Feb 17, 2004, 12:53:33 PM2/17/04
to
Albert Reiner <alb...@chello.at> writes:

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

0 new messages