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

• To: mathgroup at smc.vnet.net
• Subject: [mg46971] RE: [mg46952] doing things on a procedural way and doing them on a functional way
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Thu, 18 Mar 2004 01:24:56 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```>-----Original Message-----
>From: Pedrito [mailto:pedrito6 at softhome.net]
To: mathgroup at smc.vnet.net
>Sent: Wednesday, March 17, 2004 8:29 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg46971] [mg46952] doing things on a procedural way and doing them on a
>functional way
>
>
>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.
>

You may define a recursive procedure to generate the nestings:

Clear[nocnac]

nocnac[{arg_}, op_] := {arg}

nocnac[{args__}, op_] :=
Flatten[ReplaceList[{args}, {a__, b__} :>
Outer[op, nocnac[{a}, op], nocnac[{b}, op]]]]

nocnac[{1}, CenterDot]
==> {1}

nocnac[{1, 2}, CenterDot]
==> {1·2}

nocnac[{1, 2, 3}, CenterDot]
==> {1·(2·3), (1·2)·3}

nocnac[{1, 2, 3, 4}, CenterDot]
==> {1·(2·(3·4)), 1·((2·3)·4), (1·2)·(3·4), (1·(2·3))·4, ((1·2)·3)·4}

nocnac[{1, 2, 3, 4, 5}, CenterDot]
==>
{1·(2·(3·(4·5))), 1·(2·((3·4)·5)),
1·((2·3)·(4·5)), 1·((2·(3·4))·5),
1·(((2·3)·4)·5), (1·2)·(3·(4·5)),
(1·2)·((3·4)·5), (1·(2·3))·(4·5),
((1·2)·3)·(4·5), (1·(2·(3·4)))·5,
(1·((2·3)·4))·5, ((1·2)·(3·4))·5,
((1·(2·3))·4)·5, (((1·2)·3)·4)·5}

Length[nocnac[Range[#], op]] & /@ Range[10]
==> {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862}

--
Hartmut Wolf

```

• Prev by Date: RE: Re: Physical Constants package
• Next by Date: RE: doing things on a procedural way and doing them on a functional way
• Previous by thread: Re: doing things on a procedural way and doing them on a functional way
• Next by thread: RE: doing things on a procedural way and doing them on a functional way