Я новичок в Прологе - но это то, что я придумал.
list_codes([], "").
list_codes([Atom], Codes) :- atom_codes(Atom, Codes).
list_codes([Atom|ListTail], Codes) :-
atom_codes(Atom, AtomCodes),
append(AtomCodes, ",", AtomCodesWithComma),
append(AtomCodesWithComma, ListTailCodes, Codes),
list_codes(ListTail, ListTailCodes).
list_string(List, String) :-
ground(List),
list_codes(List, Codes),
atom_codes(String, Codes).
list_string(List, String) :-
ground(String),
atom_codes(String, Codes),
list_codes(List, Codes).
, который дает:
?- list_string([], S).
S = '' .
?- list_string([apple], S).
S = apple .
?- list_string([apple, orange], S).
S = 'apple,orange' .
?- list_string([apple, orange, peach], S).
S = 'apple,orange,peach' .
и:
?- list_string(L, '').
L = [] .
?- list_string(L, 'banana').
L = [banana] .
?- list_string(L, 'banana,litchi').
L = ['banana,litchi'] .