﻿ 如何从Prolog中的另一个谓词调用谓词？ - 代码日志

#### 如何从Prolog中的另一个谓词调用谓词？

1)是否内置函数(或者它们都被称为谓词？)用于简单的事情,例如最多2个数字,或数字的正弦等等……如果是这样,我该如何访问它们？

2)如何从另一个谓词中调用谓语？我写了两个叫做car和cdr的谓词. car返回列表的头部,cdr返回没有头部的列表.但是现在我想在cdr上打电话.以下是一些澄清的例子：

``````car([3,4,5,5], H). would return H = 3

cdr([3,4,5,5],L). would return L = [4,5,5]
``````

``````car(cdr[3,4,5,5]))
``````

??

``````remotely_acquainted(X, Y) :- knows(X, Z), knows(Z, Y), \+ knows(X, Y).
``````

``````X is Y / gcd(Z, 4).
``````

``````X < Y + Z
``````

Prolog将考虑对这些论点进行数值评估,然后对它们进行比较.

``````car(cdr([1,2,3]))
``````

``````car([H|_], H).
cdr([_|T], T).

proc_list(car(X), Result) :-
proc_list(X, R1),
car(R1, Result), !.
proc_list(cdr(X), Result) :-
proc_list(X, R1),
cdr(R1, Result), !.
proc_list(X, X).
``````

``````| ?- proc_list(car(cdr([1,2,3])), R).

R = 2

yes
| ?- proc_list(car(cdr(cdr([1,2,3]))), R).

R = 3

yes
| ?-
``````

``````proc_list(Term, Result) :-
Term =.. [Proc, X],               % Assumes terms have just one argument
member(Proc, [car, cdr]),         % True only on recognized terms
proc_list(X, R1),                 % Recursively process embedded term
ProcCall =.. [Proc, R1, Result],  % Construct a calling term with Result
call(ProcCall), !.
proc_list(X, X).
``````