[英]Separate list A/B -> B swi-prolog
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 ]. 我有一个列表[2/4,2 / 6,1 / 2,7/5]和我想创建仅由第二号的列表[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? 这没用,有什么建议吗?
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].
listDenom([ ],[ ]).
listDenom([_/D|T],[D|V]) :- listDenom(T,V).
Here is a solution using string_to_list
. 这是一个使用
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.