您是否在尋找這樣的條款?
% ensure(ELEMENT, LIST_IN, LIST_OUT)
% If X is already a member of L, then the result is L
ensure(X, L, L) :-
member(X, L), !.
% If not, the result is X appended to L
ensure(X, L, [X | L]).
用途:
1 ?- ensure(1, [1,2,3], O).
O = [1, 2, 3].
2 ?- ensure(2, [1,3], O).
O = [2, 1, 3].
我認爲這是諸如此類的事情,你正在尋找...
% enumerate(CONNECTIONS_IN, NODES_OUT, ARCS_OUT)
enumerate(C, N, A) :-
enum_nodes(C, [], N, 1),
create_arcs(C, N, A).
% enum_nodes(CONNECTIONS_IN, NODES_IN, NODES_OUT, START_ID)
% Fills up NODES_OUT with enums. New IDs start at START_ID...
enum_nodes([], N, N, _).
enum_nodes([A-B | T], N, NOUT, ID) :-
ensure_node(A, N, NTMP1, ID, ID1),
ensure_node(B, NTMP1, NTMP2, ID1, ID2),
enum_nodes(T, NTMP2, NOUT, ID2).
% ensure_node(NODE, NODES_IN, NODES_OUT, ID_IN, ID_OUT)
% Adds enum(ID_IN, NODE) to NODES_IN to produce NODES_OUT if NODE does not already exist in NODES_IN
ensure_node(NODE, NODES_IN, NODES_IN, ID, ID) :-
member(enum(_, NODE), NODES_IN), !.
ensure_node(NODE, NODES_IN, [enum(ID_IN, NODE) | NODES_IN], ID_IN, ID_OUT) :-
ID_OUT is ID_IN + 1.
% create_arcs(CONNECTIONS_IN, NODES_IN, ARCS_OUT).
% Create arcs - makes a list of arc(NODE_ID_1, NODE_ID_2)...
create_arcs([], _, []).
create_arcs([A-B | T], NODES, [arc(NODE_ID_A, NODE_ID_B) | TARCS]) :-
member(enum(NODE_ID_A, A), NODES),
member(enum(NODE_ID_B, B), NODES),
create_arcs(T, NODES, TARCS).
輸出示例:
1 ?- enumerate([a-b, c-d, d-a], N, A).
N = [enum(4, d), enum(3, c), enum(2, b), enum(1, a)],
A = [arc(1, 2), arc(3, 4), arc(4, 1)] .
如果你做一個trace
,你可以看到它的工作...
2 ?- trace.
true.
[trace] 2 ?- enumerate([a-b, c-d, d-a], N, A).
Call: (6) enumerate([a-b, c-d, d-a], _G2634, _G2635) ? creep
Call: (7) enum_nodes([a-b, c-d, d-a], [], _G2634, 1) ? creep
Call: (8) ensure_node(a, [], _G2740, 1, _G2742) ? creep
Call: (9) lists:member(enum(_G2731, a), []) ? creep
Fail: (9) lists:member(enum(_G2731, a), []) ? creep
Redo: (8) ensure_node(a, [], _G2740, 1, _G2742) ? creep
Call: (9) _G2747 is 1+1 ? creep
Exit: (9) 2 is 1+1 ? creep
Exit: (8) ensure_node(a, [], [enum(1, a)], 1, 2) ? creep
Call: (8) ensure_node(b, [enum(1, a)], _G2749, 2, _G2751) ? creep
Call: (9) lists:member(enum(_G2740, b), [enum(1, a)]) ? creep
Fail: (9) lists:member(enum(_G2740, b), [enum(1, a)]) ? creep
Redo: (8) ensure_node(b, [enum(1, a)], _G2749, 2, _G2751) ? creep
Call: (9) _G2756 is 2+1 ? creep
Exit: (9) 3 is 2+1 ? creep
Exit: (8) ensure_node(b, [enum(1, a)], [enum(2, b), enum(1, a)], 2, 3) ? creep
Call: (8) enum_nodes([c-d, d-a], [enum(2, b), enum(1, a)], _G2634, 3) ? creep
Call: (9) ensure_node(c, [enum(2, b), enum(1, a)], _G2758, 3, _G2760) ? creep
Call: (10) lists:member(enum(_G2749, c), [enum(2, b), enum(1, a)]) ? creep
Fail: (10) lists:member(enum(_G2749, c), [enum(2, b), enum(1, a)]) ? creep
Redo: (9) ensure_node(c, [enum(2, b), enum(1, a)], _G2758, 3, _G2760) ? creep
Call: (10) _G2765 is 3+1 ? creep
Exit: (10) 4 is 3+1 ? creep
Exit: (9) ensure_node(c, [enum(2, b), enum(1, a)], [enum(3, c), enum(2, b), enum(1, a)], 3, 4) ? creep
Call: (9) ensure_node(d, [enum(3, c), enum(2, b), enum(1, a)], _G2767, 4, _G2769) ? creep
Call: (10) lists:member(enum(_G2758, d), [enum(3, c), enum(2, b), enum(1, a)]) ? creep
Fail: (10) lists:member(enum(_G2758, d), [enum(3, c), enum(2, b), enum(1, a)]) ? creep
Redo: (9) ensure_node(d, [enum(3, c), enum(2, b), enum(1, a)], _G2767, 4, _G2769) ? creep
Call: (10) _G2774 is 4+1 ? creep
Exit: (10) 5 is 4+1 ? creep
Exit: (9) ensure_node(d, [enum(3, c), enum(2, b), enum(1, a)], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 4, 5) ? creep
Call: (9) enum_nodes([d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2634, 5) ? creep
Call: (10) ensure_node(d, [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2776, 5, _G2778) ? creep
Call: (11) lists:member(enum(_G2767, d), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (11) lists:member(enum(4, d), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (10) ensure_node(d, [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 5, 5) ? creep
Call: (10) ensure_node(a, [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2779, 5, _G2781) ? creep
Call: (11) lists:member(enum(_G2770, a), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (11) lists:member(enum(1, a), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (10) ensure_node(a, [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 5, 5) ? creep
Call: (10) enum_nodes([], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2634, 5) ? creep
Exit: (10) enum_nodes([], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 5) ? creep
Exit: (9) enum_nodes([d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 5) ? creep
Exit: (8) enum_nodes([c-d, d-a], [enum(2, b), enum(1, a)], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 3) ? creep
Exit: (7) enum_nodes([a-b, c-d, d-a], [], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], 1) ? creep
Call: (7) create_arcs([a-b, c-d, d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2635) ? creep
Call: (8) lists:member(enum(_G2776, a), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (8) lists:member(enum(1, a), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Call: (8) lists:member(enum(_G2777, b), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (8) lists:member(enum(2, b), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Call: (8) create_arcs([c-d, d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2774) ? creep
Call: (9) lists:member(enum(_G2788, c), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (9) lists:member(enum(3, c), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Call: (9) lists:member(enum(_G2789, d), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (9) lists:member(enum(4, d), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Call: (9) create_arcs([d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2786) ? creep
Call: (10) lists:member(enum(_G2800, d), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (10) lists:member(enum(4, d), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Call: (10) lists:member(enum(_G2801, a), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Exit: (10) lists:member(enum(1, a), [enum(4, d), enum(3, c), enum(2, b), enum(1, a)]) ? creep
Call: (10) create_arcs([], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], _G2798) ? creep
Exit: (10) create_arcs([], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], []) ? creep
Exit: (9) create_arcs([d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [arc(4, 1)]) ? creep
Exit: (8) create_arcs([c-d, d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [arc(3, 4), arc(4, 1)]) ? creep
Exit: (7) create_arcs([a-b, c-d, d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [arc(1, 2), arc(3, 4), arc(4, 1)]) ? creep
Exit: (6) enumerate([a-b, c-d, d-a], [enum(4, d), enum(3, c), enum(2, b), enum(1, a)], [arc(1, 2), arc(3, 4), arc(4, 1)]) ? creep
N = [enum(4, d), enum(3, c), enum(2, b), enum(1, a)],
A = [arc(1, 2), arc(3, 4), arc(4, 1)] .
你需要展現你想達到什麼樣的一個例子查詢。該說明本身並不清楚。您可能還會顯示一條可找到的最接近答案的鏈接,並可能提出一個具體問題。 「映射和東西」並不真正覆蓋它。 – lurker
好的會做,對不起做得不好(這是我的第三次編輯,我總是儘量不要太短或太長/特定)。 – keont
您可以使用'memberchk/2'來確定元素是否在列表中。 – lurker