For Programmers: Free Programming Magazines  


Home > Archive > Prolog > October 2006 > SWI-Prolog - search in atoms









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SWI-Prolog - search in atoms
Luigi Napolitano

2006-10-20, 8:00 am

Hi all!
I would like to find a substring in an atom.

Ex.
A = this_is_an_example_of_atom.

I wold search "example" in the A atom, to know if there is the substring
"example" in the A atom.
Is it possible (with SWI-Prolog) ?

Thanks,
Luigi


Markus Triska

2006-10-20, 8:00 am

"Luigi Napolitano" <uniluigiXXX@virgilio.it> writes:

> A = this_is_an_example_of_atom.
>
> I wold search "example" in the A atom, to know if there is the substring
> "example" in the A atom.


Using ISO predicate sub_atom/5:

?- string_to_atom("example", A),
sub_atom(this_is_an_example_of_atom, _, _, _, A).

A = example ;

Best wishes!
Markus Triska
Luigi Napolitano

2006-10-20, 8:00 am


"Markus Triska" <triska@gmx.at> ha scritto nel messaggio
news:87vemf173z.fsf@gmx.at...
> Using ISO predicate sub_atom/5:
>
> ?- string_to_atom("example", A),
> sub_atom(this_is_an_example_of_atom, _, _, _, A).
>
> A = example ;


Perfect! Thanks a lot.
A last question, if it is possible...

I can't do this: sub_atom(Thing, _, _, _, A), where Thing has an undefined
content.
Is it possibile, in other way?

Thanks really.
Luigi


Markus Triska

2006-10-20, 7:02 pm

"Luigi Napolitano" <uniluigiXXX@virgilio.it> writes:

>
> I can't do this: sub_atom(Thing, _, _, _, A), where Thing has an undefined
> content.
> Is it possibile, in other way?


You can generate atoms having A as subatom like this:

char(a).
char(b).
char(c).
% add further characters here

my_sub_atom(Thing, A) :-
atom_chars(A, Cs),
length(Cs, L0),
between(L0, infinite, L),
length(Thing0, L),
append(Head, Rest, Thing0),
append(Cs, Tail, Rest),
maplist(char, Head),
maplist(char, Tail),
atom_chars(Thing, Thing0).

Yielding:

?- my_sub_atom(A, mine).

A = mine ;

A = minea ;

A = mineb ;

A = minec ;

A = amine ;

etc.


Best wishes!
Markus Triska
Luigi Napolitano

2006-10-21, 4:00 am


"Markus Triska" <triska@gmx.at> ha scritto nel messaggio
news:87ods7c651.fsf@gmx.at...
> You can generate atoms having A as subatom like this:
>
> char(a).
> char(b).
> char(c).
> % add further characters here


Ok... Perfect!
Thank you for reply.

Best regards,
Luigi


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com