Reputation: 11
I have a List [2/4,3/6,1/2,7/5] and I want to create a list consisting of only second numbers [4, 6 ,2, 5].
I was thinking something like this:
newlist(L,L2):-
newlist(L,A/B),
newlist(A/B,B),
newlist(B,L2).
That didn't work, any suggestions?
Upvotes: 0
Views: 359
Reputation: 10672
Define a predicate for a single element:
pair_to_2nd(_/B, B).
Now apply this predicate to the list:
?- maplist(pair_to_2nd, [2/4, 3/6, 1/2, 7/5], L).
L = [4, 6, 2, 5].
Upvotes: 3
Reputation: 1259
Here is a solution using string_to_list
.
splitList(A) :-
string_to_list(A, [_,_,_,A1,_,_,_,B1,_,_,_,C1,_,_,_,D1,_]),
string_to_list(A2, [A1]),
string_to_list(B2, [B1]),
string_to_list(C2, [C1]),
string_to_list(D2, [D1]),
write([A2,B2,C2,D2]).
Example:
?- splitList('[2/4,3/6,1/2,7/5]').
[4,6,2,5]
true.
Upvotes: -4