Prolog列表以逗号分隔的字符串

sel*_*lda 5 string list prolog

我有一个列表[apple, orange],我想将它转换为像"apple,orange"Prolog中的字符串.你有什么主意吗?

Grz*_*ski 6

在SWI-Prolog中,您可以简单地使用atomic_list_concat/3atom_string/2:

?- atomic_list_concat([apple, banana, oranges], ',', Atom), atom_string(Atom, String).

Atom = 'apple,banana,oranges',
String = "apple,banana,oranges".
Run Code Online (Sandbox Code Playgroud)


Kaa*_*rel 4

在 SWI-Prolog 中您可以使用with_output_to/2. 下面是两个版本,一个使用,write/1另一个使用writeq/1. 从您的问题中不清楚您需要什么样的行为。

?- List = [apple, 'ora\\nge'], with_output_to(codes(Codes), write(List)),
   format("~s", [Codes]).
[apple,ora\nge]
List = [apple, 'ora\\nge'],
Codes = [91, 97, 112, 112, 108, 101, 44, 111, 114|...].

?- List = [apple, 'ora\\nge'], with_output_to(codes(Codes), writeq(List)),
   format("~s", [Codes]).
[apple,'ora\\nge']
List = [apple, 'ora\\nge'],
Codes = [91, 97, 112, 112, 108, 101, 44, 39, 111|...].
Run Code Online (Sandbox Code Playgroud)