序言术语到字符串

发布于 2024-11-25 14:13:57 字数 187 浏览 0 评论 0原文

我有一个像这样的序言列表:

[p(X,Y,Z),r(H,G,K)] 

我想转换它 变成这样:

'p(X,Y,Z)r(H,G,K)'  

它只是一个谓词列表,应该转换为字符串。

你有什么想法吗? (我使用序言)

i have a prolog list like this:

[p(X,Y,Z),r(H,G,K)] 

and i want convert it
into this:

'p(X,Y,Z)r(H,G,K)'  

it is just a list of predicate, that should be transformed into a string.

Do you have any idea? ( I use prolog)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2024-12-02 14:13:57

我认为这样做是不可能的。声明变量后,其名称将丢失以供进一步处理。

编辑:

如果您不介意丢失名称,这样的事情是可能的。至少在 SWI-Prolog 中:

?- format(atom(A), "~w~w", [p(X,Y,Z),r(H,G,K)]).
A = 'p(_G924,_G925,_G926)r(_G931,_G932,_G933)'.

I think doing that is not possible. After you declare a variable, its name is lost for further processing.

EDIT:

Something like this is possible, if you don't mind losing the names. At least in SWI-Prolog:

?- format(atom(A), "~w~w", [p(X,Y,Z),r(H,G,K)]).
A = 'p(_G924,_G925,_G926)r(_G931,_G932,_G933)'.
早乙女 2024-12-02 14:13:57

您可以使用 variable_names 选项读取术语,该选项为您提供读取术语中 Name=Variable 对的列表。所以也许这就是您所需要的。

这是一个命令行,它读取该术语,然后将变量与其名称统一,最后按照您在问题中指定的方式将结果打印到原子中。

?- read_term(Term, [variable_names(VarNames)]), maplist(call, VarNames),
                                with_output_to(atom(Atom), maplist(write, Term)).
|: [p(X,Y,Z),r(H,G,K)].
Term = [p('X', 'Y', 'Z'), r('H', 'G', 'K')],
VarNames = ['X'='X', 'Y'='Y', 'Z'='Z', 'H'='H', 'G'='G', 'K'='K'],
Atom = 'p(X,Y,Z)r(H,G,K)'.

请注意,它会修改读取术语,因此您可能需要先复制它 (copy_term/2)。

You can read terms using the variable_names option, which gives you a list of Name=Variable pairs in the read term. So maybe this is what you need.

Here is a command line that reads the term, then unifies the variables with their names and finally prints the result into an atom in the way that you specified in your question.

?- read_term(Term, [variable_names(VarNames)]), maplist(call, VarNames),
                                with_output_to(atom(Atom), maplist(write, Term)).
|: [p(X,Y,Z),r(H,G,K)].
Term = [p('X', 'Y', 'Z'), r('H', 'G', 'K')],
VarNames = ['X'='X', 'Y'='Y', 'Z'='Z', 'H'='H', 'G'='G', 'K'='K'],
Atom = 'p(X,Y,Z)r(H,G,K)'.

Note that it modifies the read term, so you might want to make a copy of it first (copy_term/2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文