简体   繁体   English

序词到字符串

[英]prolog term to string

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) (我使用序言)

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: 至少在SWI-Prolog中:

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

You can read terms using the variable_names option, which gives you a list of Name=Variable pairs in the read term. 您可以使用variable_names选项来阅读条款,该选项为您提供阅读条款中的名称=变量对的列表。 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 ). 请注意,它会修改读取的术语,因此您可能需要先制作一个副本( copy_term/2 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM