简体   繁体   中英

Prolog concatenate all elements inside a list to make a string?

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM