List diddling
- To: mathgroup at yoda.physics.unc.edu
- Subject: List diddling
- From: Richard Mercer <richard at rmercer.wright.edu>
- Date: Tue, 13 Jul 1993 09:10:44 -0400
Martin McClain requests: >Dear MathGroupers: >I need a pure function that operates on {x,{a,b,c}}, >where x, a, b, and c are all simple lists. The output must be >{Ints[x,a],Ints[x,b],Ints[x,c]}, where Ints means Intersection. Try this one: Fudge[{x_List,lsts__List}]:= Map[(Intersection @@ #)&,Prepend[List[#],x]& /@ lsts]; x = {2,3,5,7,11,13}; a = {1,2,3,4,5}; b = {6,7,8,9,10}; c = {11,12,13,14,15}; In[1]:= Fudge[{x,{a,b,c}}] Out[1]= {{2, 3, 5}, {7}, {11, 13}} Obviously this will work with any number of lists replacing {a,b,c}. Experienced Mathematica programmers know better than to claim a "shortest" solution! Perhaps something more elegant will come along. (Incidentally, I have written a routine called ListThread which threads together lists of different lengths. I'd be glad to send it along if you wish.) Richard Mercer