Prolog-模式匹配:由运算符链接的复杂表达式

时间:2019-02-22 01:22:40

标签: prolog pattern-matching extract operator-keyword

我是序言中的新手,我想知道是否有一种方法可以替换像这样描述的元素的一部分:

S = ( tab/field = 2 ), (X/Y=V)=S.
S = ( +field > 2 ), (+Y>V)=S.

在以下示例中,我想替换表达式的一部分(X和Y部分),但保留运算符,就像这样:

( replacedValue = 2)
( replacedValue > 2)

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

?- S = (tab/field = 2), display(S), S =.. [Op, L, R], Result =.. [Op, replaced, R].
=(/(tab,field),2)
S =  (tab/field=2),
Op =  (=),
L = tab/field,
R = 2,
Result =  (replaced=2).

?- S = (+field > 2), display(S), S =.. [Op, L, R], Result =.. [Op, replaced, R].
>(+(field),2)
S =  (+field>2),
Op =  (>),
L = +field,
R = 2,
Result =  (replaced>2).