Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Need a functional process for this.

  • To: mathgroup at
  • Subject: [mg55629] Re: Need a functional process for this.
  • From: Curt Fischer <tentrillion at>
  • Date: Thu, 31 Mar 2005 01:25:23 -0500 (EST)
  • References: <d2dpdm$lsd$> <d2dv3b$8u$>
  • Sender: owner-wri-mathgroup at

Curt Fischer wrote:
> Steve Gray wrote:
>>	I have two lists, aa and bb. aa has the general form {2,5,7,9,11,...}
>>(or{{2},{5},{7},{9},{11},...}), and bb has the general form {{6,4},{9,2},{5,6},{3,8},.... If either
>>the first or second element in any sublist (pair of integers) of bb matches any element in aa, I
>>want to delete that sublist from bb. In the above example, neither member of {6,4} or {3,8} belongs
>>to aa, while at least one element of {9,2} and {5,6} belongs to aa, so bb becomes {{6,4},{3,8}}. If
>>aa had only one element, for example 7, I could do bb=Delete[bb,Position[bb,{x_,7}|{7,y_}]], but I
>>don't know how to do it for several values instead of just "7" without using a procedural loop. 
>>	What is a good functional way to do this?
>>	Thank you for any tips.
>>Steve Gray

> I have a feeling there is a better solution that involves DeleteCases[], 
> but I couldn't come up with anything off the bat.

I figured out a way to use DeleteCases that in my opinion is more 
elegant than my previous answer and should also be faster on large-scale 
problems (I think).




The trick in this case is to realize that p|q has the FullForm of 
Alternatives[p,q].  This fact gives us a handle on using | in functional 
programming-type ways.  It's instructive to see the second argument of 
DeleteCases[] evaluated on its own.


Oh yeah, in case you are not familiar with the shorthand notation, @@ 
means Apply[] and /@ means Map[].  And the # and & symbols construct a 
pure function.

Curt Fischer

  • Prev by Date: Re: Re: Simplifying ArcTan
  • Next by Date: Re: Re: Questions about Abs[_]
  • Previous by thread: Re: Need a functional process for this.
  • Next by thread: Re : Position function