Reputation: 195
I have a term (more accurately an atom) like this:
name1(value1),name2(value2)
and I would like to have instead a "real" list like this:
[name1(value1), name2(value2)]
or separeted terms like this:
name1(value1)
and name2(value2)
any idea on how to do it?
Upvotes: 2
Views: 2195
Reputation: 71065
simple: for your list [H|T]
with T=[]
and H
the compound term in question that is its head element, have H=','(A,B), List=[A,B]
.
You can even just write H=(A,B), List=[A,B].
- parentheses are mandatory here.
IOW the data term you're talking about is just an ordinary compound term, with ',' as its functor. If you don't know the structure of these terms in advance, you can inspect it with `=../2':
H =.. [Functor | Arguments].
(I see you got the same advice from @mat).
Upvotes: 1
Reputation: 60004
In Prolog we use pattern matching to apply different processing to list'elements:
change_list([], []). % end of recursion
change_list([(T1,T2)|Ri], [T1,T2|Ro]) :- % recursive step doing required transformation
!, change_list(Ri, Ro).
change_list([E|Ri], [E|Ro]) :- % catch all rule: copy elem
change_list(Ri, Ro).
Upvotes: 1
Reputation: 40768
What about:
ands((A,B)) --> !, ands(A), ands(B).
ands(X) --> [X].
Example:
?- phrase(ands((a,b,c,d)), Ls).
Ls = [a, b, c, d].
Upvotes: 3