Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace

Status
Not open for further replies.

mkel84

Programmer
Sep 12, 2010
11
IT
I have a string "hi (pippo)" and would like a feature that I replace the parenthesis with spaces and become "hi pippo
 
Looking on google I found this:

replace( T1, S1, S2, T2 ) :-
segment( T1, Pre, S1, Post ),
append( S2, Post, S2_plus_Post ),
append( Pre, S2_plus_Post, T2 ).


segment( T, Pre, S, Post ) :-
segment_1( S, T, Pre, Post ).


segment_1( [], L, [], L ) :- !.

segment_1( [H|T_], [H|T], [], Post ) :-
segment_1( T_, T, [], Post ),
!.

segment_1( S, [H|T], [H|U], Post ) :-
segment_1( S, T, U, Post ).


append( [], L, L ).

append( [H|T], L, [H|T1] ) :-
append( T, L, T1 ).

I added this to the top:

start :- String = 'hi(pippo)', nl, write(String), nl,
replace('(', String, ' ', New), write(New).

but I do not work.
 
It seems to me you have messed up the order of parameters, and also you used ' instead of " to enclose strings. Simple quotes enclose normal atoms, not string, you need double quotes to indicate a string

Code:
replace( T1, S1, S2, T2 ) :-
    segment( S1, Pre, T1, Post ),
    append( S2, Post, S2_plus_Post ),
    append( Pre, S2_plus_Post, T2 ).

segment( List, [], [], List ) :- !.
segment( [H|T_], [], [H|T], Post ) :-
	segment( T_, [], T, Post ), !.
segment( [H|T_], [H|U], Seg, Post ) :-
	segment( T_, U, Seg, Post ).

start :-
	String = "hi(pippo)",
	replace("(", String, " ", NewString1),
	replace("p", NewString1, "c", ResultString),
	atom_chars(Result, ResultString),
	write(Result).

In the replace predicate, S1 is the entire string "hi(pippo)" and T1 is the substring to be replaced (namely "("). But for the segment predicate, the order is slightly changed, therefore the error

Now this version of replace only replaces the first occurrence of T1 within S1. If there are other occurrences, they won't be touched.
 
Strings are lists, so maplist is usefull in this case.
In SWI-Prolog, this code works too :
Code:
replace(LstOld, LstNew, S1, S2 ) :-
    maplist(replace_word(LstOld, LstNew), S1, S2).

replace_word(LstOld, LstNew, W1, W2) :-
    nth0(Ind, LstOld, [W1]),
    nth0(Ind, LstNew, [W2]).

replace_word(_LstOld, _LstNew, W, W).

remove(LstWord, S1, S2 ) :-
    exclude(remove_word(LstWord), S1, S2).


remove_word(LstWord, Word) :-
    member([Word], LstWord).

start :-
    String = "hi(pippo)",
    replace(["(", "p"], [" ", "c"], String, ResultString1),
    remove([")", ":", "!"], ResultString1, ResultString2),
    atom_chars(Result, ResultString2),
    write(Result).
 
Excuse me how do I transform the variables into a string? I implemented this:

write_result(String) :-
replace("(", String, " è ", NewString1),
replace(")", NewString1, ".", ResultString),
atom_chars(Result, ResultString),
write(Result).

write_body(N,(First,Rest)) :-
tab(N), write_result(First),nl,
write_body(N,Rest).
write_body(N,Last) :-
tab(N), write_result(Last),nl.

how(Goal) :-
clause(Goal,Body),
write_body(4,Body).

How do I make First and Last in the string?
 
Can you be more specific?

You can obtain a string in Prolog by calling a predicate and giving it a string, like this:

Code:
test :-
        call_predicate("string to test"), ...

Or you can read an atom from keyboard and convert it into a string using atom_chars:

Code:
read_string :-
        read(Atom),
        atom_chars(Atom, AtomAsString),
        writef("%s", [AtomAsString]).

Your question is not very clear to me, I hope my answer is useful, if not, please clarify your question.
 
In "write_result(String)" I pass a string, but First and Last no string.
So I would have those two variables as strings. If you read the code and try it you maybe it is easier to understand my problem, otherwise try to ask what is not clear.

Thank you very much for your help.
 
So what are First and Last because it's not clear from the code above?

If First and Last are normal atoms like george, or michael, then you can turn them into strings using atom_codes

?- atom_codes(george, X).

X will be "george"
 
Unfortunately does not work. In the variables First and Last are values like "hi (foo)" and "hi (george)" but both are not strings, I do not know what they are.
 
That's too bad. In that case, show the code that puts those values there.
 
I can not understand what kind of given me pull out the function clause. You've never used this thing to show you the steps that the interpreter has made to reach the goal?
 
I don't quite understand.

The main question that I have is simple: who creates strings like "hi(pippo)" and the like? Do you read these from input (keyboard) or what?
 
No from input (keyboard) but from variable Body that is filled with clause.
 
:)

I finally understand. So you have a clause defined somewhere, like:

Code:
test :-
        hi(pippo),
        hi(george).

... and you call 'how' on the 'test' clause. That was something not very obvious for me from your code.

The body of test contains terms separated by commas. So they are terms, not strings. The 'replace' predicate works with strings, that's why it doesn't work on your terms

My first idea to fix it would be to use term_to_atom to convert terms to atoms and then atom_codes to convert atoms to strings. Then you can use 'replace' on the strings:

Code:
:-dynamic(hi/1).
test :-
	hi(pippo),
	hi(george).

write_result(Term) :-
	[COLOR=red]
	term_to_atom(Term, Atom),
	atom_codes(Atom, String),
	[/color]
	replace("(", String, " ", NewString1),
	replace(")", NewString1, ".", ResultString),
	atom_codes(Result, ResultString),
	write(Result).

write_body(N,(First,Rest)) :-
	tab(N),
	write_result(First),
	nl,
	write_body(N,Rest).
write_body(N,Last) :-
	tab(N),
	write_result(Last),
	nl.

how(Goal) :-
	clause(Goal,Body),
	write_body(4,Body).

Now you can call how(test):

?- how(test)
hi pippo.
hi george.
true

"(" is replaced with " " and ")" is replaced with "."
 
hola.gif

You've done a great job !
 
Great, you were a great help.
Thank you very much. [thumbsup2] [thumbsup2] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top