Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog list to comma separated string

I have a list like [apple, orange]and I want to convert it to a string like "apple,orange" in Prolog. Do you have any idea?

like image 955
selda Avatar asked May 04 '11 07:05

selda


5 Answers

In SWI-Prolog you can simply use atomic_list_concat/3 and atom_string/2:

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

Atom = 'apple,banana,oranges',
String = "apple,banana,oranges".
like image 176
Grzegorz Adam Kowalski Avatar answered Nov 12 '22 00:11

Grzegorz Adam Kowalski


In SWI-Prolog you can use with_output_to/2. Below are two versions, one using write/1 and the other writeq/1. It's not clear from your question what kind of behaviour your require.

?- 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|...].
like image 37
Kaarel Avatar answered Nov 11 '22 23:11

Kaarel


if you use swi-prolog you can also do the following:

1) use the string_to_list/2 (you can give a list and get a string or give a string and get a list). the problem is that it wont insert commas so you will have to insert commas between the elements of your list manually; something like

insert_commas([],[]).
insert_commas([H|T],[H,', '|TC]):-
   insert_commas(T,TC).

so your predicate would be something like:

list_string_with_commas(L,S):-
   insert_commas(L,LC),
   string_to_list(S,LC).

2)you can use swritef/3 and string_concat/3. swritef/3 works like writef/2 but instead of writing in the output it creates a string with the data.

list_string_with_commas([],"").
list_string_with_commas([H,T],S):-
    swritef(SH,"%t, ",[H]),
    list_string_with_commas(T,ST),
    string_concat(SH,ST,T).

you might want to do some tail recursion optimisations

like image 41
Thanos Tintinidis Avatar answered Nov 12 '22 00:11

Thanos Tintinidis


I am a Prolog novice - 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'] .
like image 43
Gavi Lock Avatar answered Nov 12 '22 00:11

Gavi Lock


term_string (30 char min S.O.)

like image 1
user10221289 Avatar answered Nov 12 '22 01:11

user10221289