Q:

Use the return of a function

I'm new to Prolog and I have great problem understanding some of it. For example, I have this 'function' :
smstofre([], []).
smstofre([T|R], [X|Y] :- frword(T,X), smstofre(R,Y).

with the following 'database' :
frword(bjr,bonjour).

This is use to translate a poorly written French sentence into a correct one.
I use it like that :
?- smstofre([bjr],E).
E = [bonjour].

This is cool, but I want to create a 'function' that will take a simple string, split his word and send back the same result has the previous 'function'.
So for example :
?- translate('bjr cb ?', E).

should return :
E = [bonjour,combien,?].

I tried to use the swi-prolog's function atomic_list_concat to split the string and create a function that would do everything else, so I did that :
reduireFosse(E, F) :- atomic_list_concat(F,' ', E), smstofre(F, E).

and it just return false.
How to make it work ?

A:

I guess your reduireFosse/2 procedure should read:
reduireFosse(E, G) :- atomic_list_concat(F,' ', E), smstofre(F, G).

Note the second argument of the head of reduireFosse uses a new fresh variable G which is used in the second argument of smstofre.
And the second clause of smstofre/2 should read:
smstofre([T|R], [X|Y]) :-
  (frword(T,W) -> X = W  ; X = T), smstofre(R,Y).

Here we use -> to avoid leaving a choicepoint when the call to frword/2 succeeds, and otherwise just returns the word as is.

