我已經定義了以下運算符:序言 - 翻譯運營商
:- op(200, xfx, plus).
:- op(100, xfx, times).
我想實現一個謂語翻譯/ 2,這樣僅由數字給定的期限和經營者以及和時間,使這些運營商將被轉換爲相應的算術運算符:+和*。
例如:
?- translate((4 plus 5), Result).
Result = 4 + 5.
?- translate(((3 times 2) times 7 plus 4), Result).
Result = (3 * 2) * 7 + 4.
?-translate((5 times 3 plus 4 times 5)), Result).
Result = 5 * 3 + 4 * 5.
更新:還好吧,使代碼伊夫拿出到目前爲止有是:
replace([], _, _, []).
replace([El1|List], El1, El2, [El2|Result]) :-
replace(List, El1, El2, Result), !.
replace([H|List], El1, El2, [H|Result]) :-
replace(List, El1, El2, Result). %replaces an element in a list with
%another element.
translate(Term, ListResult2):-
Term =.. ListResult,
replace(ListResult, times, *, ListResult1),
replace(ListResult1, plus, +, ListResult2). %This will make a list of a
% a term and replace the
%operators the right way.
對不起,沒有在我的條款是如何工作增添了更多的具體意見。這裏的想法是生成一個列表,其中te操作符被相應的術語替換。我的問題是輸出是一個列表而不是一個術語。有沒有辦法扭轉我用=操作符做的事情?
你嘗試過自己嗎?什麼不起作用?你遇到什麼問題? –