prolog 是否有 spread/splat/*args 运算符?

byx*_*xor 4 prolog variadic-functions splat

在许多过程语言(例如 python)中,我可以“解包”一个列表并将其用作函数的参数。例如...

def print_sum(a, b, c):
  sum = a + b + c
  print("The sum is %d" % sum)

print_sum(*[5, 2, 1])
Run Code Online (Sandbox Code Playgroud)

此代码将打印:“ The sum is 8

这是此语言功能文档

prolog 有类似的功能吗?

有没有办法在 Prolog 中复制这种参数解包行为?

例如,我想在将列表变量传递给call之前解压缩它。

我可以写一个这样的谓词吗?

assert_true(Predicate, with_args([Input])) :-
  call(Predicate, Input).

% Where `Input` is somehow unpacked before being passed into `call`.
Run Code Online (Sandbox Code Playgroud)

...然后我可以查询

?- assert_true(reverse, with_args([ [1, 2, 3], [3, 2, 1] ])).
% Should be true, currently fails.

?- assert_true(succ, with_args([ 2, 3 ]).
% Should be true, currently fails.

?- assert_true(succ, with_args([ 2, 4 ]).
% Should be false, currently fails.
Run Code Online (Sandbox Code Playgroud)

笔记

  • 你可能认为这是一个XY 问题。有可能,但不要气馁。收到我的问题标题的答案是理想的

  • 你可能会告诉我,我对这个问题的处理很糟糕。我知道你的意图是好的,但这种建议无助于回答这个问题。请参考以上几点。

  • 也许我在过多的程序思维中接近Prolog。如果是这样,那么什么样的心态可以帮助我解决问题?

  • 我正在使用 SWI-Prolog。

fal*_*lse 6

内置的(=..)/2(univ) 用于此目的。例如

?- G =.. [g, 1, 2, 3].
G = g(1,2,3).

?- g(1,2,3) =.. Xs.
Xs = [g,1,2,3].
Run Code Online (Sandbox Code Playgroud)

但是,请注意,(=..)/2参数数量固定的地方的许多用途可以替换为call/2... call/8