0

Eg: ["c","h","a","r"] should print "char".

its atomic_list_concat(), but how would I do it for eg:

longest_common_prefix([H1,H2|T], P) :-
    maplist(append(P), L, [H1,H2|T]).
false
  • 10,264
  • 13
  • 101
  • 209
blazing
  • 557
  • 2
  • 4
  • 20

2 Answers2

5

Using library(double_quotes) as described here, we can use append/2 (note the 2!) :

?- set_prolog_flag(double_quotes).
   true.

?- append(["c","h","a","r"], Cs).
   Cs = "char".

However, please note that "c" is not a character! It is a list with one character. One single character is c alone. And most of the times, you do not need to write "c"

?- [c,h,a,r] = "char".
   true.
false
  • 10,264
  • 13
  • 101
  • 209
1

in SWI-Prolog

?- atomic_list_concat( ["c","h","a","r"] , L).
L = char.

but things become hairier soon... you should take the time to learn about elementary data representation for anything serious

CapelliC
  • 59,646
  • 5
  • 47
  • 90