Re: Flatten alternative
- To: mathgroup at smc.vnet.net
- Subject: [mg105490] Re: Flatten alternative
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Sat, 5 Dec 2009 05:32:21 -0500 (EST)
- References: <hfakvn$6re$1@smc.vnet.net>
On 2009.12.04. 10:32, Jezzybear wrote:
> I am trying to create a function myflat[list] to mimic the behavior of
> Mathematica's Flatten[]
> function.Example:
> In[1]:= myflat[{{{a}}, {b, c}, {d}}]
> Out[1]= {a, b, c, d}
> In[2]:= myflat[{{}, 1, {{2}}}]
> Out[2]= {1, 2}
> However in writing this function I want to use only Mathematica's
> pattern matching features, the functions
> First[], Rest[], Prepend[], ListQ[]. I am trying to do this using some
> subfunctions like creating one called myjoin
>
>
How about something like
flatten[list_List] := list //. {s___, {i___}, e___} :> {s, i, e}
flatten[{{1, 2}, {3, {4, 5}}}]