-1

How would I concatenate all the elements in a list together in prolog? I am trying to integrate it with my current function that outputs a list.

input = [a,b,c,d]

output = "abcd"

1 Answers1

2

If you input list contains characters then use this: Here list contains Characters and in X you get result back.

concate(X,List):-
   atom_chars(X,List).

For this query you get result as this:

concate(X,[a,b,c,d]).
  X = abcd.

Here X gets result as atom not string.

Now if you want a string back and you list contains atoms then you use this:

atomsToString(List,X):-
    atomics_to_string(List,X).

With this query:

?- atomsToString([a,b,c,d],X).
X = "abcd".

Here X contains a string.

Luai Ghunim
  • 976
  • 7
  • 14