Re: Functional programming?
- To: mathgroup at smc.vnet.net
- Subject: [mg92066] Re: Functional programming?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 19 Sep 2008 05:15:14 -0400 (EDT)
On 9/18/08 at 7:25 AM, bertmayo at earthlink.net (bertmayo) wrote:
>David Bailey says:
>>I think you have chosen a rather favourable example here. Suppose
>>instead that you need to remove every zero together with the
>>preceding element (sweeping from left to right).
>A quick and dirty fp answer is (safer code would check that leading
>elements were not already zero)
>In[1]:= v = {1, 2, 0, 3, 4, 0};
>v1 = MapAt[0 &, v, Position[v, 0] - 1];
>v2 = Select[v1, # != 0 &]
>
>Out[1]= {1, 3}
Even more concise
In[15]:= v //. {{a___, _, 0, b___} -> {a, b},{0, b___} -> {b}}
Out[15]= {1,3}
But perhaps pattern matching isn't considered part of functional programing