Re: does not work?
- To: mathgroup at smc.vnet.net
- Subject: [mg109463] Re: does not work?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 28 Apr 2010 01:59:36 -0400 (EDT)
On 4/27/10 at 7:41 AM, dims12 at hotmail.com (Dims) wrote: >I wrote a function to check if two lists contain equal members: >Intersects[x_List, >y_List] := (Do[(Print["Outer step ", xi]; >Do[(Print[xi, yi]; >If[xi == yi, (Print["Returning True"]; Return[True])]), {yi, >y}]), {xi, x}]; False) >But it returns False for interecting lists: It sounds like you intended each of the Do loops to access each list element by element. If I have this correct the following modifications to your code should work as you intended Intersects[x_List, y_List] := (Do[(Print["Outer step ", xi]; Do[(Print[x[[xi]], y[[yi]]]; If[x[[xi]] == y[[yi]], (Print["Returning True"]; Return[True])]), {yi, Length@y}]), {xi, Length@x}]; False) But while this corrects the code to do what I believe you are asking for this is not how I would approach this problem. It appears you intend for the output to be True whenever both lists contain at least one element in common. If so, the following should do what you want. In[1]:= intersects[x_List, y_List] := If[Intersection[Flatten@x, Flatten@y] == {}, False, True] In[2]:= intersects[{a, b}, {c, a}] Out[2]= True