使用 SWI-cpp.h 创建列表

Create a list using SWI-cpp.h

我正在尝试使用 SWI-cpp.h 在 C++ 中使用 SWI-PL 引擎。 我没有任何教程,我只使用 this reference,我正在 GitHub 回购中寻找一些例子。

除了一件事,我的代码工作正常;调用这个定义: foo([bar]). 如您所见,定义需要一个列表。

由于我无法将 PlTermv 放入 PlTermv,因此我不知道如何构建列表。我可以使用 PlTail,但我只知道一种必须添加空头的方法。

到目前为止,这是我的 CPP 代码:

PlTermv av(1);

// Load external pl file that contaions the foo definition.
PlCall("consult", PlTerm("foobar.pl"));

PlAtom bar = PlAtom("hello");
// I want Prolog to return a list with this compound.
av[0] = PlCompound("foo", PlTermv(bar));
PlCall("call", av); // So that this statement returns 'true' instead of 'false'.

我怎样才能以 PlCall("call",av); returnstrue 的方式更改此代码?

可以使用 atomic_list_concat/3 谓词在 Prolog 中创建列表。

这意味着 PlCall 必须在代码中再调用一次,其结果必须存储在 PlTerm:

PlTermv av(1);

// Load external pl file that contaions the foo definition.
PlCall("consult", PlTerm("foobar.pl"));

PlTerm list;

// Use space as separator. Sets the value of 'list' to '[bar]'.
PlCall("atomic_list_concat", PlTermv(list, " ", "bar"));

av[0] = PlCompound("foo", PlTermv(list));
PlCall("call", av); // returns 'true'