Reputation: 107
I have the following prolog predicate:
processWords([hello, my, name, is, Simon], Result).
I need to know how I could get:
Result = [bye, my, name, is, Ben]
How would I recurse through the list and return a string that replaces "hello" with "bye" and "Simon" with "Ben", using Prolog?
Any help is greatly appreciated, thanks!
Upvotes: 0
Views: 454
Reputation: 683
First of all, this cannot be done, since "Simon" and "Ben" in your example are variables. But supposing you are fine with "simon" and "ben", here is an answer:
processWords([], []).
processWords([H|T], [H2|T2]) :-
translate(H, H2),
processWords(T, T2).
translate(hello, bye):-!.
translate(simon, ben):-!.
translate(X, X). % catch-all clause for all words not to be translated
Alternatively, you could use maplist/3
:
processWords(L,L2):-maplist(translate, L, L2).
translate(hello, bye):- !.
translate(simon, ben):- !.
translate(X, X). % catch-all clause for all words not to be translated
Upvotes: 3