MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: pure function puzzle

Martin McClain <wmm at> writes:

> 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.  You would think this could be based on
> Thread, but when a,b,c,and x are all lists, Thread seems to get confused. 
> Map also has a problem: it needs a function as its argument, and apparently
> nested pure functions are not allowed.  I can do the required
> transformation with a compound statement

> f1 = Intersection[x,#]&;
> f2 = Map[f1,#]&

> and then apply f2 to my input, but x is not known ahead of time.  I want
> this function as one step in a long Composition, and I really need a clean,
> single function of a single argument, producing a single object. Maybe some
> clever use of Hold, or Evaluate, or something like that ???  Any ideas?

Here's one fairly simple solution:

   In[38]:= f = Apply[ Intersection, 
                       Transpose[{Table[#[[1]], {Length[#[[2]]]}], #[[2]]}],
                       1 ] &;

   In[39]:= list = {{1, 3, 5, 7}, {{1, 2, 3}, {3, 4, 5, 6}, {5, 6, 7, 8}}};

   In[40]:= f[list]

   Out[40]= {{1, 3}, {3, 5}, {5, 7}}

On the other hand, if you're willing to forego the requirement that f be a
pure function, here's a much more straightforward definition:

   In[46]:= f2[ {x_, y_} ]:= (Intersection[ x, # ]&) /@ y;

   In[47]:= f2[list]

   Out[47]= {{1, 3}, {3, 5}, {5, 7}}

Jack Lee
Dept. of Mathematics
University of Washington
Seattle, WA

  • Prev by Date: Displaying PS files
  • Next by Date: Mathematica Music
  • Previous by thread: pure function puzzle
  • Next by thread: re: pure function puzzle