我已经定义了以下运算符:序言 - 翻译运营商
:- 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操作符被相应的术语替换。我的问题是输出是一个列表而不是一个术语。有没有办法扭转我用=操作符做的事情?
你尝试过自己吗?什么不起作用?你遇到什么问题? –