RE: Understanding Flatten

• To: mathgroup at smc.vnet.net
• Subject: [mg46330] RE: [mg46314] Understanding Flatten
• From: "David Park" <djmp at earthlink.net>
• Date: Sat, 14 Feb 2004 22:19:41 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Harold,

I think this is another one of the quixotic legacy examples in The Book. In
your modified example you have to Flatten FIRST with respect to List, and
THEN Flatten with respect to f.

Flatten[ { {a, f[b, c], f[a, b, d]}, {g, f[e, g]} }, 1]
Flatten[%, 1, f]
{a, f[b, c], f[a, b, d], g, f[e, g]}
{a, b, c, a, b, d, g, e, g}

But I don't understand the general usefulness of this technique. I have
never used it. (So maybe we'll get a bunch of examples showing how useful it
is!) This was put in the book when Sequence was still an undocumented
feature. Sequence is the more direct method to splice arguments into an
expression.

{a, f1, f2} /. {f1 -> Sequence[b, c], f2 -> Sequence[a, b, d]}
{a, b, c, a, b, d}

Or

{a, f[b, c], f[a, b, d]} /. f -> Sequence
{a, b, c, a, b, d}

Another example...

Off[Plot::argmu]
Plot[f] /. f -> Sequence[Sin[x], {x, 0, 2Pi}];

David Park

From: Harold Noffke [mailto:Harold.Noffke at wpafb.af.mil]
To: mathgroup at smc.vnet.net

Mathematica 5.0.1 on Windows 2000
MathGroup:

In my study of Flatten, the Mathematica Book gives this example ...

You can use Flatten to "splice" sequences of elements into lists
or other expressions.

In[5]:= Flatten[ {a, f[b, c], f[a, b, d]}, 1, f ]

Out[5]= {a,b,c,a,b,d}

I modified In[5] as follows ...

In[1]:= Flatten[ { {a, f[b, c], f[a, b, d]}, {g, f[e, g]} }]

Out[1]= {a, f[b, c], f[a, b, d], g, f[e, g]}

I don't see why adding {g, f[e, g]} as a second list to the In[5] example
unflattens Flatten's answer.  What am I misunderstanding?

Thanks.
Harold

```

• Prev by Date: RE: The Scan Built-In Function
• Next by Date: Re: how to explain this weird effect? Integrate
• Previous by thread: Re: Understanding Flatten
• Next by thread: RE: Understanding Flatten