0

I recently started learning prolog in university(1 week in) and have a question!

So for example.

intersect([a,b,d], [b,c,a,l], L).

should output:

L=[a,b] or L=[b,a].

I've been trying for hours yet can't get it to work. I want to figure it out on my own, but I could really use some pointers in the right directions.

BTW: I'm only allowed to use member/2 as a built-in. I'm not allowed to use any other built-in predicates.

PEREZje
  • 2,272
  • 3
  • 9
  • 23
  • 3
    Can you show what you have tried? What the problem is you have encountered? Do you have - regardless of implementing it in Prolog - an idea how such algorithm should work? – Willem Van Onsem Sep 08 '17 at 14:32
  • @WillemVanOnsem. Yes, I believe first I need to create some basecases, which I did. '([ ], [ ], [ ]).' This makes is so that if the lists provided are empty, prolog will return an empty lists. Then I created a basecase, '([X], [X], [X]).'. This basically says that if the 2 lists consist of a sole member, and that member is the same. Then prolog should return said member. Then I got stuck. I have created programs that checks a list for X, and removes the first instance of X it finds and then stops. I have also created a program that checks a list for X, and removes all instances of X it finds. – PEREZje Sep 08 '17 at 16:05
  • If you have some example of what you've tried, please edit your question and update it. Do not put code in comments. – lurker Sep 08 '17 at 16:59

1 Answers1

0
inters([H1|T1],L2,[H1|L]):-
                        member(H1,L2),
                        inters(T1,L2,L).
inters([H1|T1],L2,L3):-
                        not(member(H1,L2)),
                        inters(T1,L2,L3).
inters([],_,[]).

and you can do your question: ?- inters([a,b,d],[b,c,a,l],L). as you see i 've removed all the spaces that you had in between the parameters.