I'm new to Prolog, but this is what I came up with.
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).
which gives:
?- 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' .
and:
?- list_string(L, ''). L = [] . ?- list_string(L, 'banana'). L = [banana] . ?- list_string(L, 'banana,litchi'). L = ['banana,litchi'] .