3
$\begingroup$

I can't figure out how to use fold/table/map/distribute or whatever to get this:

{f[f[{},a],a],f[f[{},a],b],f[f[{},a],c],f[f[{},a],d],f[f[{},a],e],
f[f[{},b],a],f[f[{},b],b],f[f[{},b],c],f[f[{},b],d],f[f[{},b],e],
f[f[{},c],a],f[f[{},c],b],f[f[{},c],c],f[f[{},c],d],f[f[{},c],e],
f[f[{},d],a],f[f[{},d],b],f[f[{},d],c],f[f[{},d],d],f[f[{},d],e],
f[f[{},e],a],f[f[{},e],b],f[f[{},e],c],f[f[{},e],d],f[f[{},e],e]}

That is I need to evalute f[{},n] n=a,...,e. For each result I need do the same again and again. So total evaluations for i iterations should be:

Sum[Power[5,n],{n,i}]
$\endgroup$
0

1 Answer 1

4
$\begingroup$
L = {a,b,c,d,e};
f @@@ Tuples@{f[{},#]&/@L, L}

does 5 + 25 evaluations and gives

{f[f[{},a],a],f[f[{},a],b],f[f[{},a],c],f[f[{},a],d],f[f[{},a],e],
 f[f[{},b],a],f[f[{},b],b],f[f[{},b],c],f[f[{},b],d],f[f[{},b],e],
 f[f[{},c],a],f[f[{},c],b],f[f[{},c],c],f[f[{},c],d],f[f[{},c],e],
 f[f[{},d],a],f[f[{},d],b],f[f[{},d],c],f[f[{},d],d],f[f[{},d],e],
 f[f[{},e],a],f[f[{},e],b],f[f[{},e],c],f[f[{},e],d],f[f[{},e],e]}
$\endgroup$
2
  • $\begingroup$ Nest[f @@@ Tuples@{#, L} &, f[{}, #] & /@ L, i - 1] is 30% faster than my own code thank you :D $\endgroup$
    – Coolwater
    Commented Nov 30, 2013 at 9:14
  • $\begingroup$ Nest[f @@@ Tuples@{#, test} &, {{}}, i] even better $\endgroup$
    – Coolwater
    Commented Nov 30, 2013 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.