Gamsh
Gamsh

Reputation: 545

Get all list elements in prolog

I need to get all list element in out side of the list using prolog. ['1','2','3',a,b,c] to become X='1','2','3',a,b,c I need the result X as shown above.

is it possibile to get the result?

Upvotes: 0

Views: 2556

Answers (1)

lurker
lurker

Reputation: 58324

This will literally display what you're asking for (I'm assuming you're interested in the result strictly for display purposes).

print_list(L) :-
    write('X='),
    print_list_aux(L), !.

print_list_aux([H1,H2|T]) :-
    print(H1),
    write(','),
    print_list_aux([H2|T]).
print_list_aux([X]) :-
    print(X),
    nl.
print_list_aux([]) :- nl.

Example:

?- print_list(['1', '2', a, b]).
X='1','2',a,b
true.

?- 

If you use write/1 instead of print/1, you eliminate the quotes on the numbers in the output, so it would be: X=1,2,a,b.

Upvotes: 2

Related Questions