RE: doing things on a procedural way and doing them on a functional way

• To: mathgroup at smc.vnet.net
• Subject: [mg46973] RE: [mg46952] doing things on a procedural way and doing them on a functional way
• From: "David Park" <djmp at earthlink.net>
• Date: Thu, 18 Mar 2004 01:24:58 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Pedro,

I don't know if this is the most elegant solution, but it's a solution. I
copied MapLevelParts out of the Tensorial package for use here. It will map
a function onto a sequence of level parts in an expression.

MapLevelParts[func_,
part : {toppart___Integer?Positive,
subp : {_Integer?Positive, eprest__Integer?Positive}}][expr_] :=
Module[{work, subparts, npos, null, i, nnull = Length[{eprest}]},
work = func@Part[expr, Sequence @@ part];
newparts = {work, Table[null[i], {i, 1, nnull}]} // Flatten;
npos = Partition[Range[nnull + 1], 1];
ReplacePart[expr, newparts, subparts, npos] /. null[_] -> Sequence[]
]

The following recursive routine generates all the parentheses partitions of
your elements. Unfortunately it generates some duplicates so I take the
Union. I use CircleTimes as the operator.

gennext[inlist_List] :=
Module[
{wlist = inlist, l = Length[inlist], i},
Which[
l == 2, cases = Join[cases, {CircleTimes @@ wlist}],
True,
Do[gennext[MapLevelParts[CircleTimes @@ # &, {{i, i + 1}}][wlist]],
{i,
1, l - 1}]]]

cases = {};
gennext[{a, b, c, d, e}];
cases = Union[cases]

I'm not displaying the output because of all the CircleTimes, but if we
convert CircleTimes back to List we obtain

{{a, {b, {c, {d, e}}}},
{a, {b, {{c, d}, e}}},
{a, {{b, c}, {d, e}}},
{a, {{b, {c, d}}, e}},
{a, {{{b, c}, d}, e}},
{{a, b}, {c, {d, e}}},
{{a, b}, {{c, d}, e}},
{{a, {b, c}}, {d, e}},
{{a, {b, {c, d}}}, e},
{{a, {{b, c}, d}}, e},
{{{a, b}, c}, {d, e}},
{{{a, b}, {c, d}}, e},
{{{a, {b, c}}, d}, e},
{{{{a, b}, c}, d}, e}}

David Park

From: Pedrito [mailto:pedrito6 at softhome.net]
To: mathgroup at smc.vnet.net

Hello Mathgroup!

I wanted to figure how to obtain all the possible combinations of five
numbers and four non-conmutative non-asociative operations.

This means:
if we have numbers: a b c d e
and the non-conmutative non-asociative operation:  ?

it's possible to make the following (different) expressions:
a ? (b ? (c ? (d ? e)))
a ? (b ? (c ? d) ? e))
...
(a ? b) ? (c ? d) ? e

For practical reasons you can think that the operation ? is ^ (power).

I have already obtained (by hand) all the possible combinations:
lst={{a, {b, {c, {d, e}}}}, {a, {b, {{c, d}, e}}}, {a, {{b, c}, {d,
e}}}, {a, {{b, {c, d}}, e}}, {a, {{{b, c}, d}, e}}, {{a, b}, {c, {d, e}}},
{{a, b}, {{c, d}, e}}, {{a, {b, c}}, {d, e}}, {{{a, b}, c}, {d, e}}, {{a,
{b, {c, d}}}, e}, {{a, {{b, c}, d}}, e}, {{{a, b}, {c, d}}, e}, {{{a, {b,
c}}, d}, e}, {{{{a, b}, c}, d}, e}}

And then I transformed them into an expressions by:
Module[{x = 1},
ToExpression[
StringReplace[
ToString[#1], {"{" -> "(", "}" -> ")",
"," :> {"^", "^", "^", "^"}\[LeftDoubleBracket]
x++\[RightDoubleBracket]}]]] & /@ lst

If you can help me to do it better...

Pedro L.

```

• Prev by Date: RE: doing things on a procedural way and doing them on a functional way
• Next by Date: RE: List of Variables and manipulating them ...
• Previous by thread: RE: doing things on a procedural way and doing them on a functional way
• Next by thread: RE: Re: doing things on a procedural way and doing them on a functional way