Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list with mutual elements 1

Status
Not open for further replies.

xristiana

Programmer
Oct 23, 2010
48
GR
Hi, sorry for the many posts, but I have lots of programms to solve... Now I have to solve a programm with two lists, that have at least one mutual element. I tried this without using append, but it's wrong. Can anyone help me? Thanks in advance. Here's my programm:
mutual_element([Head1|_],[Head1, _]).
mutual_element(A, [Head2|B]) :- Head1\=Head2, common_list([Head1|A], B).
 
Try using member this time.

Ask this at the Prolog prompt:

?- member(X, [1, 2, 3, 4, 5]).

X = 1 ;
X = 2 ;
X = 3 ;
X = 4 ;
X = 5

You'll figure out the rest
 
Thank you, I did this:
mutual([Head|A], [Head|B]).
mutual([Head|A], B) :- member(Head,B), common_list(A,B).
But it says singleton variables A,B. What's wrong?
 
You always have to wonder yourself what you want to express when you write a Prolog clause. Your first clause says that a list [Head | A] and a list [Head | B] have a common element, but this case is very particular, I would say. The singleton warning tells you that you don't need to use names A and B, because those variables are not referred again in the clause so you should use '_' instead of them.

Try a different approach:

Two lists A and B have a common element if there is an element X such that X is member in A and X is member in B. That's it, one simple clause.

This approach is somewhat similar with your previous test if list A is inside list B: list A is inside list B if there is a list C such that C + _ = B and _ + A = C.
 
I ran my programm and it isn't working. I was trying to compare the first element with the other three from the other list,then the second element from the first list with the three elements of the second, etc. Can I do that?
Now, I made this programm:
mutual(A, B) :- member(L, A), member (L, B).
but it's still not working. What am I doing wrong? :S
 
When I put for example in Swi-prolog, mutual([3,4],[1,2,3]),it says ERROR: toplevel: Undefined procedure: mutual/2 (DWIM could not correct goal). And also, when it compiles the programm at the beginning, it says
2/d.pl:1:0: Syntax error: Operator expected

:S
 
:) Well that's because you left a space between member and (L, B).
 
Oh, ok:) Thanks :) And I was trying to figure out! Thank you very much, I have another one programm and this is the end for now. :)
 
Hello,
very interesting forum.
I want to solve a program with 2 lists which return
true if have 2 exactly mutual elements.

Thank you for help
 
PostZach1986 >> you must create a new discussion.
Anyway, what have you already written ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top