i have an atom like 'id1,id2,id3'
and i want to split it into list
with same way as the predicate atomic_list_concat/3 in SWI .
expected result
?- atomic_list_concat(L, ',', 'id1,id2,id3').
L = [id1, id2,id3]
?- atomic_list_concat([id1,id2,id3], ',', A).
A = 'id1,id2,id3'
anyway, is it possible to do it using DCG
without any regard to efficiency, this seems fairly similar
rationale
atomic_list_concat/3 is a convenience built-in in SWI-Prolog, I think my code doesn't cover fully the specification (mainly because I lack SICStus Prolog for testing). A noteworthy difference is the qualification of acceptable types. I'm sticking to atom instead of atomic types for list' elements, because atom_concat/3 it's an ISO built-in, as such should ban some patterns accepted by SWI-Prolog implementation.
About the code, when the type testing has decided about the direction of conversion, the implementation is simple: I opted for a simple recursive concatenation in list_atom/3, that could be easily optimized, for instance adding an accumulator and thus making it tail recursion optimized, while atom_list/3 scan left to right looking for separator, and when it find it, store in list head and get the right part for recursion.
About a simple minded DCG implementation, I used this code to test a nice abstraction list//3 implemented by dcg_util, leading to this compact code:
Would be simple enough to directly implement without list//3...