Re: Pattern Matching
- To: mathgroup at smc.vnet.net
- Subject: [mg58240] Re: [mg58215] Pattern Matching
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Fri, 24 Jun 2005 02:49:59 -0400 (EDT)
- References: <200506230933.FAA16322@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Jeff wrote:
> Given a list of numbers, t = {0, -1, 0, -1, -2, -3, -4,-3,-4,-5}, I
> would like to extract those numbers that are less than or equal to the
> starting value by, for example, at least 2, but once the first (and
> each subsequent) match is found, I would the starting value to drop by
> 2. So for the list t, the output would be {-2, -4}.
>
> I can use Cases or map using If[ ] on the list to extract those
> elements that are less than or equal to -2, but I'm having difficutly
> in figuring out how to change the starting value to be applied to
> subsequent elements of the list.
>
> Given my example, I could just extract all the multiples of 2 in the
> list, but I would like the starting value to change by a fixed
> percentage or combination of fixed number and percentage.
>
> Thanks,
> Jeff
One way is to write a function that will alter arguments, and use it in
the selection predicate.
t = {0, -1, 0, -1, -2, -3, -4,-3,-4,-5}
SetAttributes[f,HoldFirst]
f[s_,x_,diff_:-2] := If [x<=s+diff, s+=diff; True, False]
s = First[t];
In[15]:= Select[Rest[t], f[s,#]&]
Out[15]= {-2, -4}
Daniel Lichtblau
Wolfram Research
- References:
- Pattern Matching
- From: "Jeff" <jmcolon@gmail.com>
- Pattern Matching