我想构建一个简单的prolog代码

我想构建一个简单的prolog代码,prolog,Prolog,嗨,我想建立一个简单的prolog代码显示多项式 例如,如果 查询:多项式([指数(A1,A2),指数(B1,B2),指数(X1,0)]) 然后显示A1X^A2+B1X^B2+X1 有人能帮我吗?首先,考虑一下如何输出单个术语,例如 ?- write_term(exponent(x, y)). poly(X) :- poly(X, first). poly([], _). poly([X | T], IS_FIRST) :- write_plus_if_not_first(

嗨,我想建立一个简单的prolog代码显示多项式

例如,如果 查询:
多项式([指数(A1,A2),指数(B1,B2),指数(X1,0)])

然后显示A1X^A2+B1X^B2+X1


有人能帮我吗?

首先,考虑一下如何输出单个术语,例如

?- write_term(exponent(x, y)).
poly(X) :-
    poly(X, first).

poly([], _).

poly([X | T], IS_FIRST) :-
    write_plus_if_not_first(IS_FIRST),
    write_term(X),
    poly(T, not_first).
1 ?- trace.
true.

[trace] 1 ?- poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]).
   Call: (6) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]) ? creep
   Call: (7) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)], first) ? creep
   Call: (8) write_plus_if_not_first(first) ? creep
   Exit: (8) write_plus_if_not_first(first) ? creep
   Call: (8) write_term(exponent(a1x, a2)) ? creep
   Call: (9) write(a1x) ? creep
a1x
   Exit: (9) write(a1x) ? creep
   Call: (9) write(^) ? creep
^
   Exit: (9) write(^) ? creep
   Call: (9) write(a2) ? creep
a2
   Exit: (9) write(a2) ? creep
   Exit: (8) write_term(exponent(a1x, a2)) ? creep
   Call: (8) poly([exponent(b1x, b2), exponent(x, 0)], not_first) ? creep
   Call: (9) write_plus_if_not_first(not_first) ? creep
   Call: (10) write(' + ') ? creep
 + 
   Exit: (10) write(' + ') ? creep
   Exit: (9) write_plus_if_not_first(not_first) ? creep
   Call: (9) write_term(exponent(b1x, b2)) ? creep
   Call: (10) write(b1x) ? creep
b1x
   Exit: (10) write(b1x) ? creep
   Call: (10) write(^) ? creep
^
   Exit: (10) write(^) ? creep
   Call: (10) write(b2) ? creep
b2
   Exit: (10) write(b2) ? creep
   Exit: (9) write_term(exponent(b1x, b2)) ? creep
   Call: (9) poly([exponent(x, 0)], not_first) ? creep
   Call: (10) write_plus_if_not_first(not_first) ? creep
   Call: (11) write(' + ') ? creep
 + 
   Exit: (11) write(' + ') ? creep
   Exit: (10) write_plus_if_not_first(not_first) ? creep
   Call: (10) write_term(exponent(x, 0)) ? creep
   Call: (11) write(x) ? creep
x
   Exit: (11) write(x) ? creep
   Exit: (10) write_term(exponent(x, 0)) ? creep
   Call: (10) poly([], not_first) ? creep
   Exit: (10) poly([], not_first) ? creep
   Exit: (9) poly([exponent(x, 0)], not_first) ? creep
   Exit: (8) poly([exponent(b1x, b2), exponent(x, 0)], not_first) ? creep
   Exit: (7) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)], first) ? creep
   Exit: (6) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]) ? creep
true .

[trace] 2 ?- 
…应该输出

x^y
……还有

?- write_term(exponent(x, 0)).
…应该只输出
x

这一位的代码是

% Only need to output base number when exponent is zero
write_term(exponent(X, 0)) :- !,
    write(X).

% Otherwise, write X^Y (the 'cut' in the previous rule will stop Prolog searching for alternatives, so once it has matched the pattern exponent(_, 0), it will know to not bother with alternatives)
write_term(exponent(X, Y)) :-
    write(X), write('^'), write(Y).
如果您定义了上述子句,现在可以执行

?- write_term(exponent(x, 2)).
x^2
但是,如果使用变量(以大写字母开头,如X1),它将不起作用

4 ?- write_term(exponent(X, 2)).
_G2395^2
因此,您只能使用以小写字母开头的“变量”来实现此功能

现在您需要一些东西来检查列表中的每个元素,并使用每个元素调用
write\u term

% Stopping case. Stop when we have an empty list...
poly([]).
% Go through each element calling 'write_term'...
poly([X | T]) :-
    write_term(X),
    poly(T).
现在如果你这样做

?- poly([exponent(a, b), exponent(c, d)]).
…你会得到

a^bc^d
 + a^b + c^d
a1x^a2 + b1x^b2 + x
我们需要打印一个加号,这样我们就可以更改第二个
poly
子句

poly([X | T]) :-
    write(' + '),
    write_term(X),
    poly(T).
现在我们将得到

a^bc^d
 + a^b + c^d
a1x^a2 + b1x^b2 + x
我们不想输出第一个“加号”,所以我们可以向子句传递一个标志,告诉它这是第一个术语,例如

?- write_term(exponent(x, y)).
poly(X) :-
    poly(X, first).

poly([], _).

poly([X | T], IS_FIRST) :-
    write_plus_if_not_first(IS_FIRST),
    write_term(X),
    poly(T, not_first).
1 ?- trace.
true.

[trace] 1 ?- poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]).
   Call: (6) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]) ? creep
   Call: (7) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)], first) ? creep
   Call: (8) write_plus_if_not_first(first) ? creep
   Exit: (8) write_plus_if_not_first(first) ? creep
   Call: (8) write_term(exponent(a1x, a2)) ? creep
   Call: (9) write(a1x) ? creep
a1x
   Exit: (9) write(a1x) ? creep
   Call: (9) write(^) ? creep
^
   Exit: (9) write(^) ? creep
   Call: (9) write(a2) ? creep
a2
   Exit: (9) write(a2) ? creep
   Exit: (8) write_term(exponent(a1x, a2)) ? creep
   Call: (8) poly([exponent(b1x, b2), exponent(x, 0)], not_first) ? creep
   Call: (9) write_plus_if_not_first(not_first) ? creep
   Call: (10) write(' + ') ? creep
 + 
   Exit: (10) write(' + ') ? creep
   Exit: (9) write_plus_if_not_first(not_first) ? creep
   Call: (9) write_term(exponent(b1x, b2)) ? creep
   Call: (10) write(b1x) ? creep
b1x
   Exit: (10) write(b1x) ? creep
   Call: (10) write(^) ? creep
^
   Exit: (10) write(^) ? creep
   Call: (10) write(b2) ? creep
b2
   Exit: (10) write(b2) ? creep
   Exit: (9) write_term(exponent(b1x, b2)) ? creep
   Call: (9) poly([exponent(x, 0)], not_first) ? creep
   Call: (10) write_plus_if_not_first(not_first) ? creep
   Call: (11) write(' + ') ? creep
 + 
   Exit: (11) write(' + ') ? creep
   Exit: (10) write_plus_if_not_first(not_first) ? creep
   Call: (10) write_term(exponent(x, 0)) ? creep
   Call: (11) write(x) ? creep
x
   Exit: (11) write(x) ? creep
   Exit: (10) write_term(exponent(x, 0)) ? creep
   Call: (10) poly([], not_first) ? creep
   Exit: (10) poly([], not_first) ? creep
   Exit: (9) poly([exponent(x, 0)], not_first) ? creep
   Exit: (8) poly([exponent(b1x, b2), exponent(x, 0)], not_first) ? creep
   Exit: (7) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)], first) ? creep
   Exit: (6) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]) ? creep
true .

[trace] 2 ?- 
第一个规则(
poly(X)
)只调用规则的第二个版本,并传入第一个元素

当处理第一个元素时,poly规则递归处理列表的其余部分,它将“not_first”作为第二个参数传递

新子句
write\u plus\u if\u not\u first
在传入
first
或输出“+”时不应执行任何操作

write_plus_if_not_first(first) :- !.
    % Do nothing case
write_plus_if_not_first(_) :-
    % Write a plus sign
write(' + ').
如果你现在打电话

?- poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]).
…你会得到

a^bc^d
 + a^b + c^d
a1x^a2 + b1x^b2 + x
如果您是Prolog新手,使用
跟踪
可能会帮助您了解正在发生的事情,例如

?- write_term(exponent(x, y)).
poly(X) :-
    poly(X, first).

poly([], _).

poly([X | T], IS_FIRST) :-
    write_plus_if_not_first(IS_FIRST),
    write_term(X),
    poly(T, not_first).
1 ?- trace.
true.

[trace] 1 ?- poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]).
   Call: (6) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]) ? creep
   Call: (7) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)], first) ? creep
   Call: (8) write_plus_if_not_first(first) ? creep
   Exit: (8) write_plus_if_not_first(first) ? creep
   Call: (8) write_term(exponent(a1x, a2)) ? creep
   Call: (9) write(a1x) ? creep
a1x
   Exit: (9) write(a1x) ? creep
   Call: (9) write(^) ? creep
^
   Exit: (9) write(^) ? creep
   Call: (9) write(a2) ? creep
a2
   Exit: (9) write(a2) ? creep
   Exit: (8) write_term(exponent(a1x, a2)) ? creep
   Call: (8) poly([exponent(b1x, b2), exponent(x, 0)], not_first) ? creep
   Call: (9) write_plus_if_not_first(not_first) ? creep
   Call: (10) write(' + ') ? creep
 + 
   Exit: (10) write(' + ') ? creep
   Exit: (9) write_plus_if_not_first(not_first) ? creep
   Call: (9) write_term(exponent(b1x, b2)) ? creep
   Call: (10) write(b1x) ? creep
b1x
   Exit: (10) write(b1x) ? creep
   Call: (10) write(^) ? creep
^
   Exit: (10) write(^) ? creep
   Call: (10) write(b2) ? creep
b2
   Exit: (10) write(b2) ? creep
   Exit: (9) write_term(exponent(b1x, b2)) ? creep
   Call: (9) poly([exponent(x, 0)], not_first) ? creep
   Call: (10) write_plus_if_not_first(not_first) ? creep
   Call: (11) write(' + ') ? creep
 + 
   Exit: (11) write(' + ') ? creep
   Exit: (10) write_plus_if_not_first(not_first) ? creep
   Call: (10) write_term(exponent(x, 0)) ? creep
   Call: (11) write(x) ? creep
x
   Exit: (11) write(x) ? creep
   Exit: (10) write_term(exponent(x, 0)) ? creep
   Call: (10) poly([], not_first) ? creep
   Exit: (10) poly([], not_first) ? creep
   Exit: (9) poly([exponent(x, 0)], not_first) ? creep
   Exit: (8) poly([exponent(b1x, b2), exponent(x, 0)], not_first) ? creep
   Exit: (7) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)], first) ? creep
   Exit: (6) poly([exponent(a1x, a2), exponent(b1x, b2), exponent(x, 0)]) ? creep
true .

[trace] 2 ?- 

希望这有帮助

请更具体一些!请展示您的尝试,并就您的困境提出更具体的问题。非常感谢!这是我第一次尝试prolog。我很感激你的帮助。非常感谢。