如何在Prolog中将列表转换为字符串?
我有一个列表如何在Prolog中将列表转换为字符串?,prolog,swi-prolog,Prolog,Swi Prolog,我有一个列表L=[a+b,b+c],我想把它转换成字符串并打印输出a+bb+c 你能帮我把这个列表转换成字符串吗?我尝试在SWI Prolog中使用atomic\u list\u concat,但它为a+b提供了一个类型错误。列表的成员是复合术语,因此在调用atomic\u list\u concat之前,必须将它们设置为原子: custom_print(L) :- maplist(term_to_atom, L, L1), atomic_list_concat(L1, L2), p
L=[a+b,b+c]
,我想把它转换成字符串并打印输出a+bb+c
你能帮我把这个列表转换成字符串吗?我尝试在SWI Prolog中使用
atomic\u list\u concat
,但它为a+b
提供了一个类型错误。列表的成员是复合术语,因此在调用atomic\u list\u concat
之前,必须将它们设置为原子:
custom_print(L) :-
maplist(term_to_atom, L, L1),
atomic_list_concat(L1, L2),
print(L2).
列表中的成员是复合术语,因此在调用
原子列表\u concat
之前,必须将它们设置为原子:
custom_print(L) :-
maplist(term_to_atom, L, L1),
atomic_list_concat(L1, L2),
print(L2).
在SWI序言中:
?- with_output_to(atom(Atom), maplist(write, [a+b, b+c])).
Atom = 'a+bb+c'.
如果您需要更多地控制术语(例如,a+b
)的编写方式,可以使用对自定义谓词的调用来替换write
。在SWI Prolog中:
?- with_output_to(atom(Atom), maplist(write, [a+b, b+c])).
Atom = 'a+bb+c'.
如果您需要更多地控制术语(例如,
a+b
)的编写方式,可以用调用自定义谓词来替换write
。可能是:您实际上想要更好的表示。可能是:您实际上想要更好的表示。