I trying to write a Prolog script that can create a list of strings that would after a simple procedure result in a given string. My knowledge of Prolog is very limited and I am not sure it is even able to perform this, so please tell me if it's impossible.
I got this so far
replace_word(Old, New, Orig, Replaced) :-
atomic_list_concat(Split, Old, Orig),
atomic_list_concat(Split, New, Replaced).
It can perform this operation
10 ?- replace_word('a','e','glava',X).
X = gleve.
But it cannot backtrack it
11 ?- replace_word('a','e',X,'gleve').
ERROR: atomic_list_concat/3: Arguments are not sufficiently instantiated
I can imagine what causes the problem, but is there a way around it?
replace_word/4 was written that way for efficiency, due to SWI-Prolog first class handling of atoms. To 'reverse' its semantic, I can't see a better way than using an helper predicate:
insert_alt/3 it's a bit more general than required, instead of a list (Ks) the pair of Old-New could be used....
test: