Related
So my problem is this i have a predicate called insere_ordenado(El,Lst1,Lst2) in which El is a random integer, Lst1 is a list and Lst2 is the resulting list of adding El to Lst1.
The thing is i did this recursively but now i have to do this iteratively and the problem is when i want to add a number that is bigger or equal than the biggest number of the list it doesnt add anything.
Example:
?- insere_ordenado_ite(2,[1,2,3,6,9],L2).
L2 = [1, 2, 2, 3, 6, 9].
?- insere_ordenado_ite(5,[1,2,3,6,9],L2).
L2 = [1, 2, 3, 5, 6, 9].
?- insere_ordenado_ite(0,[1,2,3,6,9],L2).
L2 = [0, 1, 2, 3, 6, 9].
?- insere_ordenado_ite(9,[1,2,3,6,9],L2).
L2 = [1, 2, 3, 6, 9] .
?- insere_ordenado_ite(10,[1,2,3,6,9],L2).
L2 = [1, 2, 3, 6, 9] .
As you can see in the example that i want to add 9 or 10 its supposed to add 9 or 10 (depending on the example) at the end.
Program:
insere_ordenado_ite(El,L1,L2) :- insere_ordenado_ite(El,L1,L2,[]).
insere_ordenado_ite(El,[],L2,L2).
insere_ordenado_ite(El,[P|R],L2,AC) :- El >= P,
append(AC,[P],NAC),
insere_ordenado_ite(El,R,L2,NAC).
insere_ordenado_ite(El,[P|R],L2,AC) :- El < P,
append(AC,[El,P],NAC),
append(NAC,R,NNAC),
insere_ordenado_ite(El,[],L2,NNAC).
I think the problem is that something is missing from the stop condition, but I'm not sure what it is or how to do it exactly, honestly any help would be appreciated.
How about this, which I happen to have on hand.
It's as iterative as possible, there is nothing which happens after an insert_help/3, call. The tail of the difflist is constrained to a valid list eventually, and as variables are really global, this fact is immediately visible to whoever shares the variable, in this case insert/3.
insert(P,List,T) :-
DiffList=[-1|T]-T, % -1 is a guard, smaller than anything, removed later
insert_help(P,List,DiffList).
insert_help(P,[Px|R],H-T) :-
P>Px,!,
format("go right at ~q into ~q, difflist: ~q\n",[P,[Px|R],H-T]),
T=[Px|TT],
insert_help(P,R,H-TT).
insert_help(P,[P|R],H-T) :-
!,
format("~q exists in ~q, difflist: ~q\n",[P,[P|R],H-T]),
T=[P|R].
insert_help(P,[Px|R],H-T) :-
P<Px,!,
format("insert-before at ~q into ~q, difflist: ~q\n",[P,[Px|R],H-T]),
T=[P,Px|R].
insert_help(P,[],H-T) :-
!,
format("insert ~q at end, difflist: ~q\n",[P,H-T]),
T=[P].
:-begin_tests(inserting).
test(1) :- insert(10,[],R),R=[10].
test(2) :- insert(11,[2,3,5,7],R),R=[2,3,5,7,11].
test(3) :- insert(2,[3,5,7,11],R),R=[2,3,5,7,11].
test(4) :- insert(3,[2,3,5,7,11],R),R=[2,3,5,7,11].
test(5) :- insert(3,[2,5,7,11],R),R=[2,3,5,7,11].
test(6) :- insert(7,[2,3,5,11],R),R=[2,3,5,7,11].
test(7) :- insert(2,[],R),R=[2].
:-end_tests(inserting).
rt :- run_tests(inserting).
Addendum: Solving it with append/{2,3}
If you have append/3 (and append/2), you can just do:
insert_with_append(El,List,Out) :-
search(El,List,0,Result),
((Result >= 0) % -1 means entry exists
->
(length(Front,Result), % create list of fresh variables of length Result
append(Front,Back,List), % rip list apart into Front, Back
append([Front,[El],Back],Out)) % build result
;
Out = List % already exists in list
).
% search/4 looks for the insert position, counting the number of
% items in List that come before Element, and setting CounterOut
% to that value, once known. CounterOut is set to -1 if Element
% is already in the list.
% search(Element,List,CounterCur,CounterOut)
search(_,[],Counter,Counter) :- !.
search(E,[L|Ls],CounterCur,CounterOut) :-
L<E,!,
succ(CounterCur,CounterNext),
search(E,Ls,CounterNext,CounterOut).
search(E,[L|_],CounterCur,CounterCur) :-
E<L,!.
search(E,[E|_],_,-1).
:-begin_tests(insert_with_append).
test(1) :- insert_with_append(10,[],R),R=[10].
test(2) :- insert_with_append(11,[2,3,5,7],R),R=[2,3,5,7,11].
test(3) :- insert_with_append(2,[3,5,7,11],R),R=[2,3,5,7,11].
test(4) :- insert_with_append(3,[2,3,5,7,11],R),R=[2,3,5,7,11].
test(5) :- insert_with_append(3,[2,5,7,11],R),R=[2,3,5,7,11].
test(6) :- insert_with_append(7,[2,3,5,11],R),R=[2,3,5,7,11].
test(7) :- insert_with_append(2,[],R),R=[2].
:-end_tests(insert_with_append).
rt :- run_tests(insert_with_append).
I want to sum all list elements greater than some given number. Here's the description:
sumup(L, N, GREATN, GEN) sums up the members of list L which are greater than GREATN to a variable N and puts these members into the list GEN.
Sample query:
?- sumup([8, 6, 10, 3, 9, 12], N, 7, GEN).
GEN = [8, 10, 9, 12], % expected answer
N = 39. % 8+10+9+12 = 39
Following is my code:
sum_list([], 0).
sum_list([H|T], Sum) :-
H > 3,
sum_list(T, Rest),
Sum is H + Rest.
sum_list([H|T], Sum) :-
H < 3,
write('').
I've tried the recursive way but I failed. How can I fix it?
Looking at your question and your code, I noticed a few things:
While you speak of "numbers" several times, your samples are integer-only. May we neglect non-integer numbers (float, rational) and handle integers only? I guess so.
There is an auto-loaded SWI-Prolog library(lists) predicate sum_list/2.
Calling your predicate sum_list/2 is an unfortunate choice.
Let's pick another name!
Your definition of sum_list/2 comprises three clauses:
sum_list([], 0).
Okay!
sum_list([H|T], Sum) :- H > 3, sum_list(T, Rest), Sum is H + Rest.
Notice H > 3? Why hardcode the constant integer 3?
sum_list([H|T], Sum) :- H < 3, write('').
That clause is not recursive. We need to see all list elements to calculate the sum, not stop at the first list element H that fulfills H < 3!
What's the use of write('')? I don't see any.
What good is the goal H < 3? Like above, why hardcode the integer 3?
Clause #2 covers H > 3. Clause #3 covers H < 3. What about H = 3?
In this answer we use clpfd, which is present in swi-prolog.
Here's a straight-forward definition of sumup/4 based on clpfd. While it could be improved in several ways (better determinism, accumulator-style, possibly some clever redundant constraints could also help), but for the time being it's a nice first shot:
:- use_module(library(clpfd)).
sumup([], 0, _, []).
sumup([Z|Zs], S0, P, [Z|Xs]) :-
Z #> P,
S0 #= S+Z,
sumup(Zs, S, P, Xs).
sumup([Z|Zs], S, P, Xs) :-
Z #=< P,
sumup(Zs, S, P, Xs).
Sample query as given by the OP:
?- sumup([8,6,10,3,9,12], N, 7, GEN).
N = 39, GEN = [8,10,9,12] % expected answer
; false. % leftover useless choicepoint
No need to write recursive code! Just use tfilter/3, (#<)/3, and clpfd:sum/3 like this:
:- use_module(library(clpfd)).
sumup(Zs, S, P, Xs) :-
tfilter(#<(P), Zs, Xs),
sum(Xs, #=, S).
Sample query:
?- sumup([8,6,10,3,9,12], S, 7, Xs).
S = 39, Xs = [8,10,9,12]. % expected result
Note that above query succeeds deterministically—a clear improvement over this previous answer!
Bonus! As the implementation of sumup/4 is monotonic, we know that the solution of above query is also part of the solution set of every generalization of the query. Look for yourself!
?- sumup([8,6,10,3,9,12], S, E, Xs).
S = 48, E in inf..2 , Xs = [8,6,10,3,9,12]
; S = 45, E in 3..5 , Xs = [8,6,10, 9,12]
; S = 39, E in 6..7 , Xs = [8, 10, 9,12] % <==== solution of above query
; S = 31, E in 8..8 , Xs = [10, 9,12]
; S = 22, E in 9..9 , Xs = [10, 12]
; S = 12, E in 10..11 , Xs = [12]
; S = 0, E in 12..sup, Xs = []
; false.
In SWI-Prolog you can use a fold and simply query:
L=[8, 6, 10, 3, 9, 12], include(<(7),L,Gen), foldl(plus,Gen,0,N).
so that sumup would be written as
sumup(L,N,GreatN,Gen) :-
include(<(GreatN),L,Gen),
foldl(plus,Gen,0,N).
plus/3 is an arithmetic predicate that works well in our case.
I am thinking how to multiply all elements of two list with each other. Then I want to put all results in List3. For example,
List1 = [1,3,5].
List2 = [2,6,7].
List3should contain [1x2, 1x6, 1x7, 3x2, 3x6, 3x7, 5x2, 5x6, 5x7].
In the end;
List3 = [2, 6, 7, 6, 18, 21, 10, 30, 35].
Is it possible to do that? How to do that? I couldn't find a right way.
Why not
prod(L1, L2, LP) :-
bagof(P, X^Y^(member(X, L1), member(Y, L2), P is X * Y), LP).
Here is a rather straight-forward solution using library(lambda)
product(Xs, Ys, Ps) :-
maplist(Ys+\X^maplist({X,Ys}+\Y^YP^(YP=X*Y),Ys), Xs, PPs),
append(PPs, Ps).
So we have an outer-loop for Xs and an inner loop for Ys.
?- product([1,2,3],[4,5,6],Ps).
Ps = [1*4,1*5,1*6,2*4,2*5,2*6,3*4,3*5,3*6].
Replace (YP=X*Y) by (YP is X*Y) or (YP #= X*Y). Whatever you prefer.
A simple solution not requiring any Prolog extensions (but, of course, loosing the potential benefits of using CLP(FD)) would be:
product(List1, List2, Product) :-
% save a copy of the second list
product(List1, List2, List2, Product).
product([], _, _, []).
product([X| Xs], List2, Rest2, Product) :-
( Rest2 == [] ->
product(Xs, List2, List2, Product)
; Rest2 = [Y| Ys],
Z is X * Y,
Product = [Z| Zs],
product([X| Xs], List2, Ys, Zs)
).
This solution is tail-recursive and doesn't leave spurious choice-points.
Use dcg! "Cross product"—shown here—is just one of many applications. Proceed like this:
:- meta_predicate xproduct(4,?,?,?).
xproduct(P_4,As) -->
xproduct(P_4,As,As).
:- meta_predicate xproduct(4,?,?,?,?).
xproduct(P_4,As,Bs) -->
xproduct_aux1(As,Bs,P_4). % use 1st argument indexing for As
:- meta_predicate xproduct_aux1(?,?,4,?,?).
xproduct_aux1([] ,_ , _ ) --> [].
xproduct_aux1([A|As],Bs,P_4) -->
xproduct_aux2(Bs,[A|As],P_4). % use 1st argument indexing for Bs
:- meta_predicate xproduct_aux2(?,?,4,?,?).
xproduct_aux2([],_,_) --> [].
xproduct_aux2([B|Bs],As,P_4) -->
xproduct_(As,[B|Bs],P_4).
:- meta_predicate xproduct_(?,?,4,?,?).
xproduct_([],_,_) --> [].
xproduct_([A|As],Bs,P_4) -->
xprod_(Bs,A,P_4),
xproduct_(As,Bs,P_4).
:- meta_predicate xprod_(?,?,4,?,?).
xprod_([],_,_) --> [].
xprod_([B|Bs],A,P_4) -->
call(P_4,A,B),
xprod_(Bs,A,P_4).
Let's use clpfd and lambdas to
run the query you provided in your question:
:- use_module([library(clpfd),library(lambda)]).
?- phrase(xproduct(\X^Y^[Z|Zs]^Zs^(Z #= X*Y),[1,3,5],[2,6,7]),Fs).
Fs = [2,6,7,6,18,21,10,30,35].
Above lambdas use difference-lists explicitly;
with phrase//1 we can also use them implicitly!
?- phrase(xproduct(\X^Y^phrase(([Z],{Z #= X*Y})),[1,3,5],[2,6,7]),Fs).
Fs = [2,6,7,6,18,21,10,30,35].
clpfd enables us to do very general queries. Thanks to #PauloMoura for his suggestion! Look!
?- phrase(xproduct(\X^Y^phrase(([Z],{Z #= X*Y})),As,Bs),
[2,6,7,6,18,21,10,30,35]),
maplist(labeling([]),[As,Bs]).
As = [-2,-6,-7,-6,-18,-21,-10,-30,-35], Bs = [-1]
; As = [ 2, 6, 7, 6, 18, 21, 10, 30, 35], Bs = [ 1]
; As = [-1,-3,-5], Bs = [-2,-6,-7]
; As = [ 1, 3, 5], Bs = [ 2, 6, 7]
; As = [-1], Bs = [-2,-6,-7,-6,-18,-21,-10,-30,-35]
; As = [ 1], Bs = [ 2, 6, 7, 6, 18, 21, 10, 30, 35]
; false.
Well,First take a look on this question executing operation for each list element in swi-prolog and others to know how to do for-each operation on lists.
Second, here is the code:
prod(X,[],[]).
prod(X,[HEAD|TAIL],L) :- prod(X,TAIL,L1), W is X * HEAD, L = [W|L1].
prod2([],Y,[]).
prod2([HEAD|TAIL],Y,L) :- prod(HEAD,Y,L1), prod2(TAIL,Y,L2), append(L1,L2,L).
output:
?- prod2([1,3,5] ,[2,6,7],G).
G = [2, 6, 7, 6, 18, 21, 10, 30, 35] .
The example is like this
?- runs([3,4,5,4,2,7,5,6,6,8,3], RunList).
RunList = [[3, 4, 5], [4], [2, 7], [5, 6, 6, 8], [3]]
The list need to be broken in to a number of non-decreasing sequence of consecutive numbers. My code is:
next([],0).
next([H|_],R):-
R is H.
runs1([],[]).
runs1([H|T],R):-
runs1(T,R1),
next(T,X),
H=<X,
R = [H|R1].
runs1([H|T],R):-
runs1(T,R1),
next(T,X),
H>X,
R = [[H]|R1].
I tried lots of methods, but still do not know how to write it...
Hope someone could help me.
Thanks in advance.
For a logically pure and monotone implementation look at
my answer to related question "Extracting sequences (Lists) Prolog".
I present the meta-predicate splitlistIfAdj/3 which is based on if_/3 as proposed by #false in this answer.
splitlistIfAdj/3 ensures logical soundness while remaining deterministic when possible.
The predicate passed to splitlistIfAdj/3 has to obey the same convention as (=)/3 and memberd_truth/3.
For your case we need a definition of (#>)/3:
#>(X,Y,Truth) :- X #> Y #<==> B, =(B,1,Truth).
Let's use splitlistIfAdj/3 and (#>)/3 in the example you gave:
?- splitlistIfAdj(#>,[3,4,5,4,2,7,5,6,6,8,3],Pss).
Pss = [[3,4,5],[4],[2,7],[5,6,6,8],[3]]. % succeeds deterministically
Now let's ask a more general query:
?- splitlistIfAdj(#>,[A,B],Pss).
Pss = [[A],[B]], A#>=_X, B+1#=_X ;
Pss = [[A,B]], A#>=_Y#<==>_Z, B+1#=_Y, _Z in 0..1, dif(_Z,1).
Last, let's run the query that #lurker suggested in his comment to #rrrfer's answer:
?- splitlistIfAdj(#>, Ls, [[3,4,5],[4],[2,7],[5,6,6,8],[3]]).
Ls = [3,4,5,4,2,7,5,6,6,8,3] ;
false.
runs([], []):-!.
runs([H|T], S):-
runs(T, TS),
ins(H, TS, S).
ins(E, [], [[E]]):-!.
ins(E, [[H|T]|TL], [[E, H|T]|TL]):-
H >= E, !.
ins(E, TL, [[E]|TL]).
i'm starting up learning prolog (i use SWI-prolog) and i did a simple exercise in which i have 2 lists and i want to calculate their intersection and union.
Here is my code that works pretty well but i was asking myself if there is a better way to do it as i don't like to use the CUT operator.
intersectionTR(_, [], []).
intersectionTR([], _, []).
intersectionTR([H1|T1], L2, [H1|L]):-
member(H1, L2),
intersectionTR(T1, L2, L), !.
intersectionTR([_|T1], L2, L):-
intersectionTR(T1, L2, L).
intersection(L1, L2):-
intersectionTR(L1, L2, L),
write(L).
unionTR([], [], []).
unionTR([], [H2|T2], [H2|L]):-
intersectionTR(T2, L, Res),
Res = [],
unionTR([], T2, L),
!.
unionTR([], [_|T2], L):-
unionTR([], T2, L),
!.
unionTR([H1|T1], L2, L):-
intersectionTR([H1], L, Res),
Res \= [],
unionTR(T1, L2, L).
unionTR([H1|T1], L2, [H1|L]):-
unionTR(T1, L2, L).
union(L1, L2):-
unionTR(L1, L2, L),
write(L).
Keep in mind that i want to have just 1 result, not multiple results (even if correct) so running the code with this:
?- intersect([1,3,5,2,4] ,[6,1,2]).
should exit with:
[1,2]
true.
and not with
[1,2]
true ;
[1,2]
true ;
etc...
The same must be valid for union predicate.
As i said my code works pretty well but please suggest better ways to do it.
Thanks
Also, not sure why you're dead against cuts, so long as their removal would not change the declaritive meaning of the code, as per your link. For example:
inter([], _, []).
inter([H1|T1], L2, [H1|Res]) :-
member(H1, L2),
inter(T1, L2, Res).
inter([_|T1], L2, Res) :-
inter(T1, L2, Res).
test(X):-
inter([1,3,5,2,4], [6,1,2], X), !.
test(X).
X = [1, 2].
In the test bit where I call the code, I'm just saying do the intersection but I'm only interested in the first answer. There are no cuts in the predicate definitions themselves.
The following is based on my previous answer to Remove duplicates in list (Prolog);
the basic idea is, in turn, based on #false's answer to Prolog union for A U B U C.
What message do I want to convey to you?
You can describe what you want in Prolog with logical purity.
Using if_/3 and (=)/3 a logically pure implementation can be
both efficient (leaving behind choice points only when needed)
and monotone (logically sound with regard to generalization / specialization).
The implementation of #false's predicates if_/3 and (=)/3 does use meta-logical Prolog features internally, but (from the outside) behaves logically pure.
The following implementation of list_list_intersection/3 and list_list_union/3 uses list_item_isMember/3 and list_item_subtracted/3, defined in a previous answer:
list_list_union([],Bs,Bs).
list_list_union([A|As],Bs1,[A|Cs]) :-
list_item_subtracted(Bs1,A,Bs),
list_list_union(As,Bs,Cs).
list_list_intersection([],_,[]).
list_list_intersection([A|As],Bs,Cs1) :-
if_(list_item_isMember(Bs,A), Cs1 = [A|Cs], Cs1 = Cs),
list_list_intersection(As,Bs,Cs).
Here's the query you posted as part of your question:
?- list_list_intersection([1,3,5,2,4],[6,1,2],Intersection).
Intersection = [1, 2]. % succeeds deterministically
Let's try something else... The following two queries should be logically equivalent:
?- A=1,B=3, list_list_intersection([1,3,5,2,4],[A,B],Intersection).
A = 1,
B = 3,
Intersection = [1, 3].
?- list_list_intersection([1,3,5,2,4],[A,B],Intersection),A=1,B=3.
A = 1,
B = 3,
Intersection = [1, 3] ;
false.
And... the bottom line is?
With pure code it's easy to stay on the side of logical soundness.
Impure code, on the other hand, more often than not acts like "it does what it should" at first sight, but shows all kinds of illogical behaviour with queries like the ones shown above.
Edit 2015-04-23
Neither list_list_union(As,Bs,Cs) nor list_list_intersection(As,Bs,Cs) guarantee that Cs doesn't contain duplicates. If that bothers you, the code needs to be adapted.
Here are some more queries (and answers) with As and/or Bs containing duplicates:
?- list_list_intersection([1,3,5,7,1,3,5,7],[1,2,3,1,2,3],Cs).
Cs = [1, 3, 1, 3].
?- list_list_intersection([1,2,3],[1,1,1,1],Cs).
Cs = [1].
?- list_list_union([1,3,5,1,3,5],[1,2,3,1,2,3],Cs).
Cs = [1, 3, 5, 1, 3, 5, 2, 2].
?- list_list_union([1,2,3],[1,1,1,1],Cs).
Cs = [1, 2, 3].
?- list_list_union([1,1,1,1],[1,2,3],Cs).
Cs = [1, 1, 1, 1, 2, 3].
Edit 2015-04-24
For the sake of completeness, here's how we could enforce that the intersection and the union are sets---that is lists that do not contain any duplicate elements.
The following code is pretty straight-forward:
list_list_intersectionSet([],_,[]).
list_list_intersectionSet([A|As1],Bs,Cs1) :-
if_(list_item_isMember(Bs,A), Cs1 = [A|Cs], Cs1 = Cs),
list_item_subtracted(As1,A,As),
list_list_intersectionSet(As,Bs,Cs).
list_list_unionSet([],Bs1,Bs) :-
list_setB(Bs1,Bs).
list_list_unionSet([A|As1],Bs1,[A|Cs]) :-
list_item_subtracted(As1,A,As),
list_item_subtracted(Bs1,A,Bs),
list_list_unionSet(As,Bs,Cs).
Note that list_list_unionSet/3 is based on list_setB/2, defined here.
Now let's see both list_list_intersectionSet/3 and list_list_unionSet/3 in action:
?- list_list_unionSet([1,2,3,1,2,3,3,2,1],[4,5,6,2,7,7,7],Xs).
Xs = [1, 2, 3, 4, 5, 6, 7].
?- list_list_intersectionSet([1,2,3,1,2,3,3,2,1],[4,5,6,2,7,7,7],Xs).
Xs = [2].
Edit 2019-01-30
Here is an additional query taken from #GuyCoder's comment (plus two variants of it):
?- list_list_unionSet(Xs,[],[a,b]).
Xs = [a,b]
; Xs = [a,b,b]
; Xs = [a,b,b,b]
...
?- list_list_unionSet([],Xs,[a,b]).
Xs = [a,b]
; Xs = [a,b,b]
; Xs = [a,b,b,b]
...
?- list_list_unionSet(Xs,Ys,[a,b]).
Xs = [], Ys = [a,b]
; Xs = [], Ys = [a,b,b]
; Xs = [], Ys = [a,b,b,b]
...
With the old version of list_item_subtracted/3, above queries didn't terminate existentially.
With the new one they do.
As the solution set size is infinite, none of these queries terminate universally.
To cheat slightly less than my first answer, you could use the findall higher order predicate which gets Prolog to do the recursion for you :
4 ?- L1=[1,3,5,2,4], L2=[6,1,2], findall(X, (nth0(N, L1, X), member(X, L2)), Res).
L1 = [1, 3, 5, 2, 4],
L2 = [6, 1, 2],
Res = [1, 2].
If the aim is to just 'get the job done', then swi prolog has built in primitives for exactly this purpose:
[trace] 3 ?- intersection([1,3,5,2,4] ,[6,1,2], X).
intersection([1,3,5,2,4] ,[6,1,2], X).
X = [1, 2].
[trace] 4 ?- union([1,3,5,2,4] ,[6,1,2], X).
X = [3, 5, 4, 6, 1, 2].
Try this, analogue to union/3 here:
:- use_module(library(clpfd)).
member(_, [], 0).
member(X, [Y|Z], B) :-
(X #= Y) #\/ C #<==> B,
member(X, Z, C).
intersect([], _, []).
intersect([X|Y], Z, T) :-
freeze(B, (B==1 -> T=[X|R]; T=R)),
member(X, Z, B),
intersect(Y, Z, R).
It works if the elements are integer, and doesn't leave any choise point:
?- intersect([X,Y],[Y,Z],L).
freeze(_15070, (_15070==1->L=[X, Y];L=[Y])),
_15070 in 0..1,
_15166#\/_15168#<==>_15070,
_15166 in 0..1,
X#=Y#<==>_15166,
X#=Z#<==>_15168,
Y#=Z#<==>_15258,
_15168 in 0..1,
_15258 in 0..1.
?- intersect([X,Y],[Y,Z],L), X=1, Y=2, Z=3.
X = 1,
Y = 2,
Z = 3,
L = [2].
?- intersect([X,Y],[Y,Z],L), X=3, Y=2, Z=3.
X = Z, Z = 3,
Y = 2,
L = [3, 2].
And finally (really), you could use findall to find all the solutions, then use nth0 to extract the first one, which will give you the result you want without cuts, and keeps the predicates nice and clean, without have any additional predicates to trap/stop prolog doing what it does best - backtracking and finding multiple answers.
Edit: It's arguable that putting in extra predicates in the 'core logic' to prevent multiple results being generated, is as ugly/confusing as using the cuts that you are trying to avoid. But perhaps this is an academic exercise to prove that it can be done without using higher order predicates like findall, or the built-ins intersection/union.
inter([], _, []).
inter([H1|T1], L2, [H1|Res]) :-
member(H1, L2),
inter(T1, L2, Res).
inter([_|T1], L2, Res) :-
inter(T1, L2, Res).
test(First):-
findall(Ans, inter([1,3,5,2,4], [6,1,2], Ans), Ansl),
nth0(0, Ansl, First).
% Element X is in list?
pert(X, [ X | _ ]).
pert(X, [ _ | L ]):- pert(X, L).
% Union of two list
union([ ], L, L).
union([ X | L1 ], L2, [ X | L3 ]):- \+pert(X, L2), union(L1, L2, L3).
union([ _ | L1 ], L2, L3):- union(L1, L2, L3).
% Intersection of two list
inter([ ], _, [ ]).
inter([ X | L1 ], L2, [ X | L3 ]):- pert(X, L2), inter(L1, L2, L3).
inter([ _ | L1 ], L2, L3):- inter(L1, L2, L3).
I know this post is very old but I found a solution with minimum coding.
% intersection
intersection([],L1,L2,L3).
intersection([H|T],L2,L3,[H|L4]):-member(H,L2),intersection(T,L3,L3,L4).
% member
member(H,[H|T]).
member(X,[H|T]):-member(X,T).
To test the above code you should not enter L3. Here is an examples.
?- intersection([w,4,g,0,v,45,6],[x,45,d,w,30,0],L).
L = [w, 0, 45].