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

*To*: mathgroup at smc.vnet.net*Subject*: [mg46981] Re: doing things on a procedural way and doing them on a functional way*From*: DrBob <drbob at bigfoot.com>*Date*: Thu, 18 Mar 2004 01:25:17 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

This seems to work: Clear[done, combo1, combo2, raw, combos] combo1 = {raw[a_, b_] :> done[a, b], a_raw :> combo2[a] /@ Range[Length@a - 1]}; combo2[raw[a___, b_, c_, d___]][ n_Integer] /; Length@{a, b} == n := raw[a, done[b, c], d] combos[a_List, op_] := Union@(Flatten[raw @@ a //. combo1] /. done -> op) Some tests: combos[{a, b}, Power] combos[{a, b, c}, Power] combos[{a, b, c, d}, Power] combos[{a, b, c, d, e}, Power] combos[{a, b, c, d, e, f}, Plus] combos[{a, b, c, d, e, f}, List] It seems a tad too involved, but I don't see a better way so far. Bobby Pedrito <pedrito6 at softhome.net> wrote in message news:<c38uvb$g39$1 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.